本帖最后由 朱京辉 于 2013-1-27 06:29 编辑
using (SqlConnection conn = new SqlConnection (@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True;")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * from T_User where UserName='" + UserName + "'"; if (cmd.ExecuteNonQuery() > 0) { //返回受影响行数>0说明有用户存在 using (SqlDataReader reader = cmd.ExecuteReader()) { string dbpassword = reader.GetString(reader.GetOrdinal( "Password")); if (Password == dbpassword) { Console .WriteLine("密码正确,登录成功" ); } else { Console .WriteLine("密码错误,登录失败" ); } } } else //reader返回false,就是没有查找到这个用户名 { Console .WriteLine("用户名错误" ); } } }
输入正确的用户名和密码还是现实用户名错误
|