这是我在登录按钮后面的代码
dall login = new dall();
DataTable dt = login.select(("select ID,Password from Login"));
for (int i = 0; i < dt.Rows.Count; i++)
{
if (txtID.Text==dt.Rows[i][0].ToString() && txtPass.Text==dt.Rows[i][1].ToString())
{
MessageBox.Show("Login Successfully", "Login ALert", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Login Failed", "Login Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
}这是我的连接类代码
conn.Open();
SqlCommand cmd = new SqlCommand(query,conn);
SqlDataAdapter adptr = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adptr.Fill(dt);
conn.Close();
return dt;发布于 2015-02-27 06:35:28
我猜想,不正常工作意味着失败信息是多次出现的。
您的身份验证方法是错误的。如果ID和Password匹配,如果记录或值来自数据库,则可以从数据库中提取数据,否则显示成功,否则失败。
1)将查询更改为使用参数ID和密码。
string query = "select ID,Password from Login Where ID = @ID AND Password =@Pssword"2)然后将参数值添加到add命令中
command.Parameters.Add("@ID", SqlDbType.Int);
command.Parameters.Add("@Password", SqlDbType.String);3)然后检查DataTable是否有它的值
if (dt.Rows.Count == 0)
//Show failure message
else
//show success messagehttps://stackoverflow.com/questions/28758838
复制相似问题