现要设计一个登陆窗口,有botton1按钮、textbox1和textbox2。需要连接access数据库文件1.dmb,读取表login中的name列和psw列。
当点击botton1按钮时,验证textbox1和textbox2里输入的字符串是分别否等于name列或psw列,若其中一个不等则提示错误,否则通过。我想请问,怎么把name列和psw列的字符作为判断条件去判断是否和textbox1和textbox2输入的字符串相等?代码该如何写?
以下为已编写的代码,If条件内该怎么写?private void button1_Click(object sender, EventArgs e)
        {
            string Afile = "provider=Microsoft.Jet.OleDb.4.0;Data Source=1.mdb";
            OleDbConnection AconnStr = new OleDbConnection(Afile);
            OleDbCommand Acmd = new OleDbCommand("select name,psd from login",AconnStr);
            OleDbDataReader odr = OleDbCommand.ExecuteReader();
            AconnStr.Open();
            odr = Acmd.ExecuteReader();
            if (???)
                MessageBox.Show("账号或密码错误");
            else
            {
                Form a = new mainwindows();
                a.Show();
                this.Hide();
            }

解决方案 »

  1.   

    其实只要查询的时候根据输入内容查询就好了
    select name,psd from login where name='"+textbox1.text.tostring()+"' and psd='"+textbox2.text.tostring()+"' 如果查询结果为空,就是输入不正确
    如果查询结构不为空,就是输入正确了嘛
    不过在查询前最好先过滤下字符串哦
    防止输入为空,或是输入非法的字符最近服务器被sql注入的注入怕了以前一个同事写的80%的页面都能被sql注入
    一直在修改,头大啊...
      

  2.   

    textbox1.text=odr["表中账号字段的名"].toString();
    textbox2.text=odr["表中密码字段的名"].toString();
      

  3.   

    IF(Exists(SELECT * FROM Table WHERE name=textbox1.Text and pwd=textbox2.Text)) //判断数据库中是否存在
    BEGIN
    可以返回一个值,代表存在这条记录,也就是可以登陆
    END
      

  4.   

    select name,psd from login where name = @nameif(psd != DataTable.Rows[0]["psd"].ToString())
    {
      MessageBox.Show("账号或密码错误");
    }或者 
    select name,psd from login where name = @name and psd=@pad
    if(DataTable.Rows.Count == 0)
    {
       MessageBox.Show("账号或密码错误");}
      

  5.   

    Exists比查询出来再判断是否为空的效率要高
      

  6.   


    string Afile = "provider=Microsoft.Jet.OleDb.4.0;Data Source=1.mdb"; 
    OleDbConnection AconnStr = new OleDbConnection(Afile); 
    OleDbCommand Acmd = new OleDbCommand(string.Format("select name,psd from login where name='{0}' and psd='{1}'",AconnStr); 
    OleDbDataReader odr = OleDbCommand.ExecuteReader(); 
    AconnStr.Open(); 
    odr = Acmd.ExecuteReader(); 
    if (!odr.HasRows) 
    {                Form a = new mainwindows(); 
                    a.Show(); 
                    this.Hide(); 
    }
     else    
                 MessageBox.Show("账号或密码错误"); 
      

  7.   


    string Afile = "provider=Microsoft.Jet.OleDb.4.0;Data Source=1.mdb"; 
    OleDbConnection AconnStr = new OleDbConnection(Afile); 
    OleDbCommand Acmd = new OleDbCommand(string.Format("select name,psd from login where name='{0}' and psd='{1}'",textbox1.Text.Trim(),textbox2.Text.Trim()),AconnStr); 
    OleDbDataReader odr = OleDbCommand.ExecuteReader(); 
    AconnStr.Open(); 
    odr = Acmd.ExecuteReader(); 
    if (!odr.HasRows) 
    {   
       Form a = new mainwindows(); 
       a.Show(); 
       this.Hide(); 
    }
     else    
       MessageBox.Show("账号或密码错误"); 
    刚刚没有写完这句
      

  8.   

    odr.read判断是否有数据返回
    odr.getvalue(index)获取相应列数据