新人求助 用户验证 但是返回的总是MessageBox.Show("error")
private void button3_Click(object sender, EventArgs e)
{
if (login())
{
MessageBox.Show( "ok ");
}
else {
MessageBox.Show( "error ");
}
}
public bool login()
{string connString = @ "
server=U-UQVSSCFJKQ4JP;
integrated security=sspi;
database=公交;
";
string sql = @ "select * from admin ";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader read = cmd.ExecuteReader();
if (read.Read())
{
if (adminid.Text == read[ "admin "].ToString() && passworld.Text == read[ "passworld "].ToString()) //---与数据库进行比较
{ return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
[解决办法]
你这样只能读第一行啊
[解决办法]
1.if (read.Read()) 换成while
2.或者string sql = "select * from admin ";后面加上条件select * from admin where adminid = ' "+adminid.Text+ " ' and password = ' "+passworld.Text + " ';
[解决办法]
楼上正解
[解决办法]
private void button3_Click(object sender, EventArgs e)
{
if (login())
{
MessageBox.Show( "ok ");
}
else {
MessageBox.Show( "error ");
}
}
public bool login()
{
//新加变量
bool flag = false;
string connString = @ "
server=U-UQVSSCFJKQ4JP;
integrated security=sspi;
database=公交;
";
string sql = @ "select * from admin ";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader read = cmd.ExecuteReader();
if (read.Read())
{
if (adminid.Text == read[ "admin "].ToString() && passworld.Text == read[ "passworld "].ToString()) //---与数据库进行比较
{ flag = true; }
}
else
{
flag = false; }
return flag;
}
[解决办法]
正解
if (adminid.Text == read[ "admin "].ToString() && passworld.Text == read[ "passworld "].ToString())
改正为:
if (adminid.Text.Trim() == read[ "admin "].ToString() && passworld.Text.Trim() == read[ "passworld "].ToString())
就行了!