简体   繁体   English

连接到MDB数据库C#

[英]Connecting to the MDB Database C#

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll System.Data.dll中发生了未处理的“System.Data.OleDb.OleDbException”类型异常

Additional information: Could not use ''; 附加信息:无法使用''; file already in use This is where the error points at: 文件已被使用这是错误指向的位置:

da.Fill(dt);

The database is located at C:\\ChattBankMDB.mdb on my computer. 该数据库位于我计算机上的C:\\ChattBankMDB.mdb

Database: http://puu.sh/hjQj0/d86ede4c00.png 数据库: http//puu.sh/hjQj0/d86ede4c00.png

When I press the button1, I would like for the form to follow up and login on the Customer database else a messagebox.show will say failure to login. 当我按下button1时,我希望表单跟进并登录Customer数据库,否则messagebox.show会说登录失败。

Button on form: 表格上的按钮:

public partial class CustLogin : Form { 公共部分类CustLogin:Form {

OleDbConnection db = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ChattBankMDB.mdb");
OleDbDataAdapter da = new OleDbDataAdapter();
DataTable dt = new DataTable();
public CustLogin()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    da = new OleDbDataAdapter("Select CustID, CustPassword From Customers", db);
    da.Fill(dt);
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        if (UserText.Text == dt.Rows[i]["CustID"] && PassText.Text == dt.Rows[i]["CustPassword"])
        {
            WelcomeCust f = new WelcomeCust();
            this.Hide();
            f.Show();
        }
        else
        {

            MessageBox.Show("FAILURE TRY AGAIN");
        }
    }
}

I've noticed a couple of potential issues: 我注意到了一些潜在的问题:

  • Database Injection. 数据库注入。
  • Password in plain text. 密码以纯文本显示。
  • Utilizing SqlConnection . 使用SqlConnection

A .mdb isn't a SQL database, it is actually a Microsoft Access database. .mdb不是SQL数据库,它实际上是Microsoft Access数据库。 So you'll want to actually use ADO.NET connection. 所以你想要实际使用ADO.NET连接。 So your code should actually be: 所以你的代码应该是:

private readonly string dbConnection = ConfigurationManager.ConnectionStrings["..."].ConnectionString;
private const string query = "SELECT * FROM [Example] WHERE ([Id] = @Id);";

public void Example()
{
     using(var connection = new OleDbConnection(dbConnection))
         using(var command = new OleDbCommand(query, connection))
         {
              // Apply parameter, open connection, etc.
         }
}

You utilize parameters to avoid a sub-query being introduced. 您可以利用参数来避免引入子查询。 As for your password in plain text you should take a look at BCrypt or another library for a Salt / Hash approach. 至于你的纯文字密码,你应该看看BCrypt或其他图书馆的Salt / Hash方法。

Then the change to the connection should alleviate your issue. 然后更改连接应该可以缓解您的问题。

Your next issue I believe stems from the Fill being before you build your data table. 我认为您的下一个问题源于在构建数据表之前的Fill

The database is an Acces database , so you need to use OleDB to connect to it. 数据库是一个Acces数据库,因此您需要使用OleDB连接它。

Moreover, the query can cause errors. 而且,查询可能会导致错误。

Replace : 替换:

("Select* from Customers where CustID ='" + UserText.Text +"'
and CustPassword =" + PassText.Text + '"', conn)

By : 通过:

("Select * from Customers where CustID = '" + UserText.Text + "' 
and CustPassword = '" + PassText.Text + "'", conn);

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

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