我做了一个系统登陆界面的窗口,登陆按钮的代码如下:
private void button_Click(object sender, System.EventArgs e)
{
string sql=string.Format("select userpsw from userinfor where useraccount='{0}'",
this.textBox1.Text);
OleDbCommand cmd=new OleDbCommand(sql,this.oleDbConnection1);
this.oleDbConnection1.Open();
object x=cmd.ExecuteScalar();
if(x==null)
{
MessageBox.Show("用户名不存在");
}
else
{
if(this.textBox2.Text==x.ToString())
{
MessageBox.Show("登陆成功");

}
else
{
MessageBox.Show("密码错误");
}
}
this.oleDbConnection1.Close();
}
其中userinfor为用户表,包括usertype用户类型,useraccount为用户帐号,userpsw 为用户密码,现在我需要在界面中添加一项用户类型:下拉框comboBox1用户选择(普通用户和超级用户),用户登陆时需要选择,请问SQL怎么写?
object x=cmd.ExecuteScalar();中ExecuteScalar该改成什么??

解决方案 »

  1.   

    ExecuteScalar 返回的是记录中的第一行第一列的数据 
    ExecuteReader返回的是一个只向前的数据库记录读取工具。如果read()方法返回true表示正确读到数据,改数据就是当前记录的数据
      

  2.   


    if(this.textBox2.Text==x.ToString())
    {
    MessageBox.Show("登陆成功");

    }该怎么改呢?
      

  3.   

    //SQL 语句
    string sql = "select userpsw from userinfor where useraccount='" + this.textBox1.Text + "' and usertype='" + comboBox1.SelectedText + "'";
      

  4.   

    try
    {
    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Application.StartupPath + @"\exam.mdb");
    string sql = "select userpsw from userinfor where useraccount='" + this.textBox1.Text + "' and usertype='" + comboBox1.Text + "'";
    OleDbCommand comm = new OleDbCommand(sql, conn);
    conn.Open();
    OleDbDataReader dr = comm.ExecuteReader();
    bool flag = false;
    while(dr.Read())
    {
    if (dr["userpsw"].ToString() == this.textBox2.Text.ToString())
    {
    MessageBox.Show("你是合法用户");
    }
    else
    {
    MessageBox.Show("密码错误!");
    this.textBox2.Focus();
    this.textBox2.SelectAll();
    }
    flag = true;
    }
    if (flag==false)
    MessageBox.Show("此用户不存在,是否注册!"); conn.Close();}
    catch(Exception ee)
    { MessageBox.Show(ee.Message);
      

  5.   

    前一个回帖中SQL语句拼错,是 comboBox1.Text 而非comboBox1.SelectedText
      

  6.   

    我将ExecuteScalar 改为ExecuteReader,
    string sql=string.Format("select userpsw from userinfor where useraccount='{0}'",
    this.textBox1.Text);
    改为string sql = "select userpsw from userinfor where useraccount='" + this.textBox1.Text + "' and usertype='" + comboBox1.SelectedText + "'";
    输入数据库中已经存在的用户,类型叶选的对的,怎么老是弹出密码错误,是不是还要改其他地方啊,高手?
      

  7.   

    请参看我的回帖,是 comboBox1.Text 而非comboBox1.SelectedText