private void b1_Click(object sender, System.EventArgs e)
{
Form1 theOwner = (Form1)this.Owner ; //获得对主窗口的引用

string uname=t1.Text.ToString();
string upwd=t2.Text.ToString();
//连接数据库
string strConn="server=Localhost;uid=sa;pwd=6079197;database=test";
// 连接到eForum数据库
SqlConnection conn=new SqlConnection(strConn);
// 打开连接
conn.Open();
// 构造SQL语句,该语句在RegUsers表中检查昵称和密码是否正确
string str="select * from user where username='"+uname+"' and userpwd='"+upwd+"'";
// 创建Command对象
SqlCommand cmd=new SqlCommand(str,conn);
// 执行ExecuteReader()方法  
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read())
{
Form2 frmMain = new Form2();
frmMain.Show ();
this.Hide ();
}
else
{
MessageBox.Show ("用户名和密码错误!","提示");
}
dr.Close(); 
conn.Close();

}运行时  SqlDataReader dr=cmd.ExecuteReader(); 有问题,提示如下:未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。其他信息: 系统错误。

解决方案 »

  1.   

    conn.Open();
    // 构造SQL语句,该语句在RegUsers表中检查昵称和密码是否正确
    string str="select * from user where username='"+uname+"' and userpwd='"+upwd+"'";
    // 创建Command对象
    SqlCommand cmd=new SqlCommand(str,conn);改成:
    // 构造SQL语句,该语句在RegUsers表中检查昵称和密码是否正确
    string str="select * from user where username='"+uname+"' and userpwd='"+upwd+"'";
    conn.Open();
    // 创建Command对象
    SqlCommand cmd=new SqlCommand(str,conn);
    看看
      

  2.   

    string str="select * from user where username='"+uname+"' and userpwd='"+upwd+"'";
    这样的话还能读到什么信息
    登陆呢
    就是测试用户名或密码对与不对
    select userpawdfrom user where username=?
    cmd.Parameters.Add(New OleDbParameter("username", OleDbType.VarWChar, 50))
    cmd.Parameters["username"].value=textbox.text
    然后用参数把textbox的值传进去
    这样防止注入这样取出来的密码与用户输入的密码进行对比
    就OK了
      

  3.   

    在你调试的时候
    出现异常用try catch捕获一下,错误信息就出来了
    SqlCommand cmd=new SqlCommand(str,conn);
    // 执行ExecuteReader()方法  
    try
    {
    SqlDataReader dr=cmd.ExecuteReader();
    if(dr.Read())
    {
    Form2 frmMain = new Form2();
                      rmMain.Show ();
    this.Hide ();
    }
    }
    catch(Exception ex)
    {
    message.show(ex.tostring())
    }这样有时就能出现详细的错误信息 else
      

  4.   

    try了之后出现什么信息?
    看上面的代码,能出错的就只有
    //连接数据库
    string strConn="server=Localhost;uid=sa;pwd=6079197;database=test";
      

  5.   

    改成这样试试:
    // 构造SQL语句,该语句在RegUsers表中检查昵称和密码是否正确
    string str="select * from user where username='"+uname+"' and userpwd='"+upwd+"'";
    conn.Open();
    // 创建Command对象
    SqlCommand cmd=new SqlCommand(str,conn);
    int i = (int)cmd.ExecuteScalar();
    if(i == 0)
    {
    用户名或密码不正确
    }
    else
    {
       跳转到正确的页面(用户和密码正确);}
      

  6.   

    谢谢各位,可以了。原因不详,我把数据库表“user”改成“userinf”就可以了。