简体   繁体   中英

login function in a visual studio C# windows form connected to a MS Access database 2000 - 2003 format. Researched and tried fixing to no avail

When I attempt to run the application and insert stored username and password or random characters an error pops up instead of the intended validation message. Code Below:

    private void btnLogin_Click(object sender, EventArgs e)
    {
        //Connection Login form to SQL Database login table to get username and password
        SqlConnection sqlcon = new SqlConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\mbewe\Documents\Visual Studio 2019\VS Projects\ZRA Transport Requisition\LoginDB.mdb");
        string query = "Select * from Logininfo where Username = '" + tbUsername.Text.Trim() + "'and Password= '" + tbPassword.Text.Trim() + "'";
        SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
        DataTable dtbl = new DataTable();
        sda.Fill(dtbl);
        if (dtbl.Rows.Count == 1)
        {
            //Setting which form to open once username and password have been confirmed
            frmRD objFrmRD = new frmRD();
            this.Hide();
            objFrmRD.Show();
        }
        else
        {
            //Setting message to be shown when username and password do not exist in the database
            MessageBox.Show("Check Your Username and Password");
        }
    }

Error Shown: 在此处输入图片说明

As others said, you need to use OleDbConnection.

I modify the code you provided, which can verify the username and password successfully.

 private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\School.mdb");
            con.Open();
            string query = "Select * from Logininfo where Username = '" + tbUsername.Text.Trim() + "'and Password= '" + tbPassword.Text.Trim() + "'";
            OleDbDataAdapter sda = new OleDbDataAdapter(query, con);
            DataTable dtbl = new DataTable();
            sda.Fill(dtbl);
            if (dtbl.Rows.Count == 1)
            {
                MessageBox.Show("success");
                //Setting which form to open once username and password have been confirmed
                Form2 form = new Form2();
                this.Hide();
                form.Show();
            }
            else
            {
                //Setting message to be shown when username and password do not exist in the database
                MessageBox.Show("Check Your Username and Password");
            }


        }

Tested result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM