简体   繁体   English

连接到 MS Access 数据库 2000 - 2003 格式的 Visual Studio C# windows 窗体中的登录功能。 研究并尝试修复无济于事

[英]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.正如其他人所说,您需要使用 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:测试结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Visual C# 中修复我的 MS Access 数据库时出错 - Error fixing my MS Access Database in Visual C# 使用连接到MS Access数据库的C#中的用户角色登录 - login with user roles in C# connected to MS Access database Visual Studio 2015 中无法识别的数据库格式,在 C# 中使用 MS Access 数据库 - Unrecognised database format in Visual Studio 2015 with MS Access Database in for C# 从Visual Studio C#中的MS Access数据库中获取OLE(位图)对象形式,我的代码有什么问题? - Taking OLE (bitmap) object form MS Access database in Visual studio C# , What's wrong in my code? Visual C#对连接到Access Db的Login表单进行编程,仅给出尝试登录 - Visual C# Programming a Login form connected to a Access Db that gives only tries to log into C# WPF Access 2000-2003 文件创建 - C# WPF Access 2000-2003 Files Creation 筛选和搜索连接到C#中的MS Access数据库的DataGridView - Filter and search DataGridView connected to a MS Access database in C# Visual Studio 2003中不支持C#ParameterizedThreadStart? - C# ParameterizedThreadStart not supported in Visual Studio 2003? 从Visual Studio C#代码访问MS Access数据库中的数据 - Accessing data within MS Access database from visual studio C# Code 格式代码MS Visual Studio for C#控制台项目不起作用 - Format Code MS Visual Studio for C# Console project Not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM