日期:2014-05-19  浏览次数:20788 次

c#中连接数据库的问题
private   System.Data.SqlClient.SqlConnection   conn;
string   sql= "select   Name,Password   from   Register   where   (Name= ' "+this.textBox1.Text+ " ')and(Password= ' "+this.textBox2.Text+ " ') ";
this.sqlCommand1.CommandText=sql;
this.sqlCommand1.Connection=this.conn;

this.sqlCommand1.Connection.Open();

SqlDataReader   reader=this.sqlCommand1.ExecuteReader();
                                                                。。。。。
                                                                reader.close();
                                                                this.sqlCommand1.Connection.Close();
这样使用哪里有错啊?
提示这样的错误
未处理的“System.InvalidOperationException”类型的异常出现在   system.data.dll   中

------解决方案--------------------
conn 初始化了吗?
------解决方案--------------------
也就是说:
conn = new System.Data.SqlClient.SqlConnection();
了吗?
------解决方案--------------------
conn.Open() 少了一句
------解决方案--------------------
this.sqlCommand1.Connection.Open();
改成this.conn.Connection.Open();
------解决方案--------------------
private System.Data.SqlClient.SqlConnection conn;

在方法中需要 将 conn = new System.Data.SqlClient.SqlConnection();
------解决方案--------------------
不要括号吧
------解决方案--------------------
using System;
using System.Data.SqlClient;
class Dbconn
{
}