简体   繁体   English

如何使用C#连接到MS Access文件(mdb)?

[英]How to connect to a MS Access file (mdb) using C#?

I'm trying to connect to a mdb file and I understand that I would need Microsoft.OLEDB.JET.4.0 data provider. 我正在尝试连接到mdb文件,并且我知道我需要Microsoft.OLEDB.JET.4.0数据提供程序。 Unfortunately, I do not have it installed on the (University) machine. 不幸的是,我没有在(大学)机器上安装它。 Since, they don't provide that provider, I believe there should be a way around. 由于他们不提供该提供程序,因此我认为应该有办法解决。

How can I connect to the file without Microsoft.OLEDB.JET.4.0 or is there any alternative ? 在没有Microsoft.OLEDB.JET.4.0的情况下如何连接文件,或者有其他选择吗?

I have following providers: 我有以下提供商:

可用的Ole DB提供程序

I have tried using OLE DB Provider for Microsoft Directory Services , to which while testing connection, I get 'Test succeeded but some settings were not accepted by the provider'. 我尝试使用OLE DB Provider for Microsoft Directory Services ,在测试连接时,我得到“测试成功,但提供程序未接受某些设置”。 I took that string and used it anyway and I got ADsDSOObject' failed with no error message available, result code: DB_E_ERRORSINCOMMAND(0x80040E14) . 我采用了该字符串并使用了它,但我得到了ADsDSOObject' failed with no error message available, result code: DB_E_ERRORSINCOMMAND(0x80040E14)

The simplest way to connect is through an OdbcConnection using code like this 最简单的连接方法是使用如下代码通过OdbcConnection

using System.Data.Odbc;

using(OdbcConnection myConnection = new OdbcConnection())
{
    myConnection.ConnectionString = myConnectionString;
    myConnection.Open();

    //execute queries, etc

}

where myConnectionString is something like this 其中myConnectionString是这样的

myConnectionString = @"Driver={Microsoft Access Driver (*.mdb)};" + 
"Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;

See ConnectionStrings 请参阅ConnectionStrings

In alternative you could create a DSN and then use that DSN in your connection string 或者,您可以创建一个DSN,然后在连接字符串中使用该DSN

  • Open the Control Panel - Administrative Tools - ODBC Data Source Manager 打开控制面板-管理工具-ODBC数据源管理器
  • Go to the System DSN Page and ADD a new DSN 转到系统DSN页面并添加新的DSN
  • Choose the Microsoft Access Driver (*.mdb) and press END 选择Microsoft Access驱动程序(* .mdb),然后按END键。
  • Set the Name of the DSN (choose MyDSN for this example) 设置DSN的名称(此示例选择MyDSN)
  • Select the Database to be used 选择要使用的数据库
  • Try the Compact or Recover commands to see if the connection works 尝试执行“压缩”或“恢复”命令以查看连接是否正常

now your connectionString could be written in this way 现在您的connectionString可以用这种方式编写

myConnectionString = "DSN=myDSN;"

Here's how to use a Jet OLEDB or Ace OLEDB Access DB: 这是使用Jet OLEDB或Ace OLEDB Access DB的方法:

using System.Data;
using System.Data.OleDb;

string myConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
                           "Data Source=C:\myPath\myFile.mdb;" +                                    
                           "Persist Security Info=True;" +
                           "Jet OLEDB:Database Password=myPassword;";
try
{
    // Open OleDb Connection
    OleDbConnection myConnection = new OleDbConnection();
    myConnection.ConnectionString = myConnectionString;
    myConnection.Open();

    // Execute Queries
    OleDbCommand cmd = myConnection.CreateCommand();
    cmd.CommandText = "SELECT * FROM `myTable`";
    OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); // close conn after complete

    // Load the result into a DataTable
    DataTable myDataTable = new DataTable();
    myDataTable.Load(reader);
}
catch (Exception ex)
{
    Console.WriteLine("OLEDB Connection FAILED: " + ex.Message);
}

You should use "Microsoft OLE DB Provider for ODBC Drivers" to get to access to Microsoft Access. 您应该使用“用于ODBC驱动程序的Microsoft OLE DB提供程序”来访问Microsoft Access。 Here is the sample tutorial on using it 这是使用它的示例教程

http://msdn.microsoft.com/en-us/library/aa288452(v=vs.71).aspx http://msdn.microsoft.com/zh-CN/library/aa288452(v=vs.71).aspx

What Access File extension or you using? 或您使用什么访问文件扩展名? The Jet OLEDB or the Ace OLEDB. Jet OLEDB或Ace OLEDB。 If your Access DB is .mdb (aka Jet Oledb) 如果您的Access DB是.mdb(又名Jet Oledb)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Oledb

namespace MembershipInformationSystem.Helpers
{
    public class dbs
    {
        private String connectionString;
        private String OleDBProvider = "Microsoft.JET.OLEDB.4.0"; \\if ACE Microsoft.ACE.OLEDB.12.0
        private String OleDBDataSource = "C:\\yourdb.mdb";
        private String OleDBPassword = "infosys";
        private String PersistSecurityInfo = "False";

        public dbs()
        {

        }

        public dbs(String connectionString)
        {
            this.connectionString = connectionString;
        }

        public String konek()
        {
            connectionString = "Provider=" + OleDBProvider + ";Data Source=" + OleDBDataSource + ";JET OLEDB:Database Password=" + OleDBPassword + ";Persist Security Info=" + PersistSecurityInfo + "";
            return connectionString;
        }
    }
}

Try this.. 尝试这个..

using System.Data.OleDb;

OleDbConnection dbConn;

dConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Registration.accdb;");

Another simplest way to connect is through an OdbcConnection using App.config file like this 另一个最简单的连接方法是使用这样的App.config文件通过OdbcConnection

  <appSettings>  
    <add key="Conn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|MyDB.mdb;Persist Security Info=True"/>
  </appSettings>

MyDB.mdb is my database file and it is present in current primary application folder with main exe file. MyDB.mdb是我的数据库文件,它与主exe文件一起存在于当前的主应用程序文件夹中。

if your mdf file has password then use like this 如果您的mdf文件具有密码,则使用像这样

  <appSettings>
    <add key="Conn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|MyDB.mdb;Persist Security Info=True;Jet OLEDB:Database Password=Admin$@123"/>
  </appSettings>

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

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