首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2012-01-28 
c#中连接数据库的问题privateSystem.Data.SqlClient.SqlConnectionconnstringsql selectName,Passwordf

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

热点排行