【问题标题】:Cannot initialize the data source object of OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)"无法为链接服务器“(null)”初始化 OLE DB 提供程序“Microsoft.Jet.OLEDB.4.0”的数据源对象
【发布时间】:2015-08-06 06:05:15
【问题描述】:

我遇到了这样的错误

'无法为链接服务器 "(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# oledb


    【解决方案1】:

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

    希望这会有所帮助。

    【讨论】:

    • 感谢您的信息,但我使用的是服务器数据库而不是本地数据库
    • 您好,本示例中的目标数据库运行在服务器上(它是一个 MS SQL Server)
    • 只需更新连接字符串并使用您的服务器的名称或 IP 地址更改服务器属性。
    • 我已经写了这样的查询 select GLFIN12.* into [GLFIN12] IN [oledb];Provider=SQLOLEDB.1;Password=vipl-user!@#$%;Persist Security Info= True;User ID= vipl-user;Initial Catalog=QQQ;Server=serverpc\sqlexpress FROM [GLFIN12] 但收到错误“IErrorInfo.GetDescription failed with E_FAIL(0x80004005)”。
    • 也许是一个愚蠢的问题,但我看到您使用的是 SQL Express。是否配置为接受远程 TCP 连接?您可以使用例如 Microsoft SQL Server Management Studio 从其他机器连接到它吗?
    【解决方案2】:

    您似乎正在尝试使用错误的连接字符串连接到 SQL 服务器(我假设您正在尝试将信息从 MS Access 复制到 MS SQL Server)。

    有效的 MS SQL Server 连接字符串如下所示:Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

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

    【讨论】:

    • 我也尝试过更改连接字符串,但没有用得到同样的错误。
    • 我在这里发现了一个类似的问题:social.msdn.microsoft.com/Forums/sqlserver/en-US/…也许这会对你有所帮助?
    • 我试过这个也遇到了错误,比如执行查询'IErrorInfo.GetDescription failed with E_FAIL(0x80004005)。 ' 我的代码是 'OleDbCommand cmd = new OleDbCommand("select * into ["+tableName+"] IN "+con+" FROM "+tableName,connection); cmd.ExecuteNonQuery();连接.Close();'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 2017-08-10
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多