读书人

c#中连接数据库的有关问题

发布时间: 2012-01-28 22:06:13 作者: rapoo

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
{
}

读书人网 >C#

热点推荐