我收到了这样的错误
'无法初始化OLE DB提供程序的数据源对象" Microsoft.Jet.OLEDB.4.0"对于链接服务器"(null)"。 OLE DB提供程序" Microsoft.Jet.OLEDB.4.0"对于链接服务器"(null)"返回消息"无法找到可安装的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) { }
你能解决这个错误
吗?答案 0 :(得分:0)
看起来您正在尝试使用错误的连接字符串连接到SQL服务器(我假设您正在尝试将信息从MS Access复制到MS SQL Server)。
有效的MS SQL Server连接字符串如下所示:Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
有关连接字符串的更多信息,请查看:http://www.connectionstrings.com
答案 1 :(得分:0)
我对我的本地环境做了一个简单的测试,下面的工作就像一个魅力(这是来自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数据库作为我的目标进行了测试。
希望这有帮助。