本帖最后由 李志祥 于 2012-4-18 15:11 编辑
为什么运行时会用设置的admin,888888会提示密码错误,但是别的正常,输入错误的用户名则提示用户名错误
Console.WriteLine("请输入用户名");
string username = Console.ReadLine();
Console.WriteLine("请输入密码");
string password = Console.ReadLine();
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\黑马\代码练习\SQLserver\SQLserver\Database1.mdf;
Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from T_User where Username='"+username+"'";
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
//用户名存在
string dbpassword = reader.GetString(reader.GetOrdinal("password"));
if (password == dbpassword)
{
Console.WriteLine("登陆成功");
}
else
{
Console.WriteLine("密码错误,登陆失败");
}
}
else//Read返回false,就是没有查到这个用户名
{
Console.WriteLine("用户名错误");
}
}
}
}
Console.WriteLine("ok");
Console.ReadKey(); |