c# - 无法为链接服务器 "Microsoft.Jet.OLEDB.4.0"初始化 OLE DB 提供程序 "(null)"的数据源对象

标签 c# oledb

我遇到这样的错误

'无法为链接服务器“(null)”初始化 OLE DB 提供程序“Microsoft.Jet.OLEDB.4.0”的数据源对象。 链接服务器“(null)”的 OLE DB 提供程序“Microsoft.Jet.OLEDB.4.0”返回消息“找不到可安装的 ISAM。”。在将数据库从 msaccess 传输到 SQL 时

这段代码是我写的

try
{
    DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
    DataTable userTables = null;
    using (connection)
    {
       string mappath = dataGridView1.CurrentRow.Cells["Path"].Value.ToString();
       string[] filePaths = Directory.GetFiles(@"" + mappath + "", "*.mdb", SearchOption.TopDirectoryOnly);
       // c:\test\test.mdb
       foreach (string tr in filePaths)
       {
          connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + tr + "";
          string[] restrictions = new string[4];
          restrictions[3] = "Table";
          connection.Open();
          userTables = connection.GetSchema("Tables", restrictions);
          List<string> tableNames = new List<string>();
          for (int i = 0; i < userTables.Rows.Count; i++)
             tableNames.Add(userTables.Rows[i][2].ToString());
          try
          {
             foreach (string tableName in tableNames)
             {
                cn1 = new SqlConnection(con);
                if (cn1.State != ConnectionState.Open) { cn1.Open(); }
                SqlCommand cmd = new SqlCommand("select * into [" + tableName + "] from OPENROWSET('Microsoft.Jet.OLEDB.4.0','" + tr + "',[" + tableName + "])");
                cmd.Connection = cn1;
                cmd.ExecuteNonQuery();---Got error Here
             }
          }
          catch (Exception Ex) { connection.Close(); }
          connection.Close();
       }
    }
 }
 catch (Exception Ex) { }

你能解决这个错误吗

最佳答案

我在我的本地环境上做了一个简单的测试,下面的效果非常好(这是来自 C# 控制台应用程序):

static void Main(string[] args)
{
    string accessConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Temp\\StackOverflowDemo\\MyAccessDb.mdb;User Id=admin;Password =; ";

    using (DbConnection accessConnection = new OleDbConnection(accessConnectionString))
    {
        accessConnection.Open();

        using (DbCommand accessCommand = new OleDbCommand())
        {
            string accessQuery =
                "SELECT * INTO [MySqlTable] IN '' [ODBC;Driver={SQL Server};Server=(local);Database=MySqlDb;Uid=username;Pwd=password;] FROM [MyAccessTable]";

            accessCommand.CommandText = accessQuery;
            accessCommand.Connection = accessConnection;

            accessCommand.ExecuteNonQuery();
        }
    }
}

我已经使用 MS Access 2002 数据库作为源并使用 SQL Server 2014 数据库作为目标对其进行了测试。

希望这对您有所帮助。

关于c# - 无法为链接服务器 "Microsoft.Jet.OLEDB.4.0"初始化 OLE DB 提供程序 "(null)"的数据源对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31847932/

相关文章:

c# - 在 C# 中完全关闭一个程序

c# - 如何在 C# 中计算某个类的 <span> 实例?

c# - 引发 EndPointNotFoundException 所需的时间

.net - ExecuteReader时"Parameter ?_1 has no default value"错误

c# - 在 C# 中使用 OleDb 将数据插入 Excel 2007

sql-server - 请求的操作需要 OLE DB Session 对象... - 通过 ADO 将 Excel 连接到 SQL 服务器

c# - 如何限制与 Async HttpClient C# 的连接

c# - String.Format不适用于日期

c# - 使用 OleDbParameter 在 Access 中插入日期/时间值

sql-server - Windows Server 2016 中的访问数据库引擎 OLEDB