无法初始化OLEDB提供程序的数据源对象;对于链接服务器"(null)"

本文关键字:quot 于链接 服务器 null 链接 OLEDB 初始化 程序 对象 数据源 | 更新日期: 2023-09-27 18:06:47

出现如下错误

'无法初始化链接服务器"(null)"的OLEDB提供程序"Microsoft.Jet.OLEDB.4.0"的数据源对象。链接服务器的OLEDB提供程序"Microsoft.Jet.OLEDB.4.0" (null)"返回消息"无法找到可安装的ISAM "。

我已经写了这个代码

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) { }

请解决这个错误

无法初始化OLEDB提供程序的数据源对象;对于链接服务器"(null)"

我在我的本地环境中做了一个简单的测试,下面的工作就像一个魅力(这是来自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数据库作为我的目标。

看起来你正在尝试使用错误的连接字符串连接到SQL服务器(我假设你正试图从MS Access复制信息到MS SQL server)。

一个有效的MS SQL Server连接字符串看起来像这样:Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

有关连接字符串的更多信息,请查看:http://www.connectionstrings.com