string userid = textBox1.Text;
string password = textBox2.Text;
            string connection = "server=.;database=mydb;user id=sa;pwd=123";
            SqlConnection myconnection = new SqlConnection(connection);
            SqlCommand mycommand = myconnection.CreateCommand();
            mycommand.CommandText = "select * from 用户信息表 where 用户=''+userid+'' and 密码=''+password+''";
            myconnection.Open();
            SqlDataReader reader = mycommand.ExecuteReader();
            if (reader.HasRows == true)
            {
                MessageBox.Show("登陆成功", "登陆");
            
            }
else 
{ MessageBox.Show("登陆失败", "失败");
}这段程序中,已经没有错误,而我在所连接的数据库的表,设置用户和密码,我在程序的输入框中输入,与数据库表的密码和账号,都报错为登陆失败!请问哪里出现了错误?如何更改

解决方案 »

  1.   


    我在程序的输入框中输入,与数据库表的密码和账号.
    只能是用户信息表中没有相关的记录。 看看是否有空格。直接把字符串放到sql查询分析器中看看。"select * from 用户信息表 where 用户='" +userid+ "' and 密码='"+password+"'"
      

  2.   

     "select * from 用户信息表 where 用户=''+userid+'' and 密码=''+password+''"断点说明我这条语句 的userid 和password 列有错误,但是如果我写3个''',就不会有错,我觉得这个程序的错误在于这里!
      

  3.   

    mycommand.CommandText = "select * from 用户信息表 where 用户='" + userid + "' and 密码='" + password + "'";
      

  4.   


    sql 里面 ''表示字符串
      

  5.   

    额,淡定,LZ估计没注意你6L的代码,分赃不均了!!!只能说明,LZ喜欢直接复制替换就能运行的代码,