我正在使用VS2010构建Windows表单应用程序。我想读取用户ID和密码,并检查它们与数据库的有效性。如果找到了正确的匹配,我想将用户引导到主表单。当我输入正确的凭据时,我的代码目前没有指示。可能是什么原因?
string connectionstring = "Data Source=localhost;Initial Catalog=HMS;Persist Security Info=True;User ID=Developer;Password=abc@123";
SqlConnection connection = new SqlConnection(connectionstring);
string SelectStatement = "SELECT * FROM Users where UserID = '@UserID' and Password = '@password'";
SqlCommand insertcommand = new SqlCommand(SelectStatement, connection);
insertcommand.Parameters.AddWithValue("@UserID", textBox1.Text);
insertcommand.Parameters.AddWithValue("@password", textBox2.Text);
SqlDataReader reader;
try
{
connection.Open();
reader = insertcommand.ExecuteReader();
while (reader.Read())
{
string userid = reader["UserID"].ToString();
string pass = reader["Password"].ToString();
if (userid == textBox1.Text.ToString() && pass == textBox2.Text.ToString()) // login successful
{
Mainform main = new Mainform();
this.Hide();
main.Show();
}
else
{
MessageBox.Show("Invalid Username or Password!");
}
}//end while
reader.Close();
}
catch (Exception ex)
{
throw ex;
}//end catch
finally
{
connection.Close();
}
答案 0 :(得分:0)
首先,不要将密码作为原始文本存储在数据库中。那只是在惹麻烦。您应该在数据库中存储盐渍哈希并计算客户端中的哈希值并进行比较。
其次,尝试使用main.ShowDialog()让你的主表单保持不变。