登录系统msaccess错误komut.ExecuteReader()未处理

时间:2016-08-18 13:14:57

标签: c# ms-access login

这是我的MSacces:

enter image description here

我的错误:我可以注册并登录但是当我再次启动程序时,我无法使用相同的变量登录。

例如:我使用admin 123注册,然后使用admin 123登录。然后我关闭程序并再次打开它,我无法使用admin 123登录。

表格1从这里开始:

    OleDbConnection bağlanti = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=üye.accdb");
    private void button2_Click(object sender, EventArgs e)
    {
        bağlanti.Open();//connection open
        OleDbCommand komut = new OleDbCommand("select * From üyeler", bağlanti);
        OleDbDataReader okuyucu = komut.ExecuteReader();//reader



        while (okuyucu.Read())reader.read
        {
            if (textBox1.Text.ToString() == okuyucu["kullaniciadi"].ToString())//read[accountname]
            {
                if (textBox2.Text.ToString() == okuyucu["kullanicisifre"].ToString())//read[password]
                {

                    MessageBox.Show("tebrikler giriş başarılı");//cong sign in sucseed
                    Form2 frm = new Form2();//going new form
                    frm.Show();
                    this.Hide();
                }
            }               
            else
            {
                MessageBox.Show("Bu kullanıcı adı şifresi yanlıştır");
            }
        }
        bağlanti.Close();
    }

    private void uyeol_Click(object sender, EventArgs e)
    {
        Form3 frm = new Form3();//sign up button
        frm.Show();
        this.Hide();
    }
}
}

及其表格3

    OleDbDataAdapter da;
    OleDbCommand cmd;
    DataSet ds;

    OleDbConnection bağlanti = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =üye.accdb");

    void griddoldur()
    {
        bağlanti = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=okul.accdb");//con
        da = new OleDbDataAdapter("select *from ogrenci", bağlanti);
        ds = new DataSet();

    }
    private void Form1_Load(object sender, EventArgs e)
    {
        griddoldur();
    }
         public void button1_Click(object sender, EventArgs e)
    {
        cmd = new OleDbCommand();

        bağlanti.Open();//connection open
        cmd.Connection = bağlanti;//cmd = new OleDbCommand();
        cmd.CommandText="insert into üyeler (kullaniciadi,kullanicisifre,tel,ad) values ('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"')";//(account name + textbox1)(passw+ textbox2)(phonenumber+textbox3)(Name + textbox 4)
        cmd.ExecuteNonQuery();,//cmd = new OleDbCommand();
        bağlanti.Close();//connection close

            Form1 frm = new Form1();//going log in form
            frm.Show();
            this.Hide();

    }
}

1 个答案:

答案 0 :(得分:1)

要正确检查您的用户+密码是否存在,请使用此方法

private void button2_Click(object sender, EventArgs e)
{
    string cmdText = @"select * From üyeler 
                       where kullaniciadi=@account 
                         and kullanicisifre=@pass";
    using(OleDbConnection bağlanti = new OleDbConnection(.......))
    using(OleDbCommand komut = new OleDbCommand(cmdText, bağlanti))
    {
        bağlanti.Open();//connection open
        komut.Parameters.Add("@account", OleDbType.VarWChar).Value = textBox1.Text;
        komut.Parameters.Add("@pass", OleDbType.VarWChar).Value = textBox2.Text;
        using(OleDbDataReader okuyucu = komut.ExecuteReader())
        {
            // Now with the WHERE clause if there are rows you have the login
            if(okuyucu.HasRows)
            {
                MessageBox.Show("tebrikler giriş başarılı");//cong sign in sucseed
                Form2 frm = new Form2();//going new form
                frm.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("Bu kullanıcı adı şifresi yanlıştır");
            }
        }               
    }
}

如果存在需要用户名和密码的记录,则此查询使用WHERE子句让数据库搜索您。另请注意,我使用了参数化查询来避免解析错误和Sql注入。最后,所有一次性对象都应该包含在一个使用块中,以便在使用它们时将其销毁(特别是OleDbConnection对象)

关于你的代码,还有另一件事要说。您似乎将密码以明文形式保存在数据库中。这是一个很大的安全风险,使用Access数据库,每个人都可以简单地复制/查看文件并查看所有用户密码。不要这样做,而是搜索如何store passwords in a database