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 中

解决方案 »

  1.   

    也就是说:
    conn = new System.Data.SqlClient.SqlConnection();
    了吗?
      

  2.   

    谢谢大家!!
    这里conn.open()和this.sqlCommand1.Connection.Open();的作用不一样吗?
    我加了句conn.ConnectionString=sql;
    错误又成这样了
    其他信息:在连接子字符串“select Name,Password from Register where Name='admin' and Password='admin'”中需要“;”分隔符。这句sql语句感觉没地方错呀。困惑
      

  3.   

    this.sqlCommand1.Connection.Open(); 
                                      改成this.conn.Connection.Open();
      

  4.   

    private System.Data.SqlClient.SqlConnection conn;在方法中需要 将 conn = new System.Data.SqlClient.SqlConnection();
      

  5.   

    改成this.conn.Connection.Open();????
    还有这样的?不行吧。
      

  6.   

    using System;
    using System.Data.SqlClient;
    class Dbconn
    {
    }
      

  7.   

    那 估计 你 的 ConnetString 没写 那些 什么  server=. ;database=×××;uid=××;pwd=××× 的 写了吗 ???
    (如果是 sql 的话)
      

  8.   

    刚才看了是没写诶。然后我把之前的那个子窗口的数据连接的ConnectionString 的值直接拷贝过来粘贴到这个窗口了。这样还是一样的错误。ConnectionString的值要修改吗?
      

  9.   

    data source=340D9556CC484B3;initial catalog=bysj;integrated security=SSPI;persist security info=False;workstation id=340D9556CC484B3;packet size=4096
    它是这样的
      

  10.   

    你 在 上面 提到  你加了句:我加了句conn.ConnectionString=sql;有去掉没 ?
      

  11.   

    去掉了。我改用dataAdapter和dataSet,dataCommander,解决了。
    郁闷啊。刚才的还是不知道出什么问题。
    我是初学者,我好菜。多多学习
    谢谢个位哦