C# 系统登陆问题

来源:百度知道 编辑:UC知道 时间:2024/05/30 06:44:07
登陆按钮的代码如下~ 为什么会登陆不上呢? 使用ExecuteScalar()方法读出来的都是空的~ 怎么回事?

conn.Open();
string sql = "SELECT * FROM userinformation WHERE userid='" + tbx_userid.Text + "' and userpassword='" + tbx_userpassword + "'";
OleDbCommand cmd = new OleDbCommand(sql, conn);

if (null==cmd.ExecuteScalar())
{
label4.Text = "用户名或密码错误,请重新输入";
}
else
{
Index1 index1 = new Index1();
index1.ShowDialog();
}
conn.Close();

把你的sql语句传到这个方法里就可以了 你的原因是判断是否有查询到数据那里出错 这句 null==cmd.ExecuteScalar()

public bool BoolExec(string Sql)//执行查询SQL语句 用于 验证数据是否存在 返回true表示登陆成功
{
try
{
Cmd = new SqlCommand(Sql, this.GetCon());
return Cmd.ExecuteReader().HasRows;
}
catch (Exception e)
{
throw e;
}
finally
{
SqlCon.Close();
}
}

conn.Open();
string sql = "SELECT * FROM userinformation WHERE userid='" + tbx_userid.Text + "' and userpassword='" + tbx_userpassword + "'";
OleDbCommand cmd = new OleDbCommand(sql, conn);
int icount=(int)cmd.ExecuteScalar();
if (icount>0)
{
登录成功
}
else
{
登录失败
}
conn.Close();