我收到了这样的错误' IErrorInfo.GetDescription因E_FAIL(0x80004005)'而失败。
执行查询以将数据库从msaccess传输到SQL。
我写了这段代码
try
{
string str = "";
cn1 = new SqlConnection(con);
if (cn1.State != ConnectionState.Open) { cn1.Open(); }
str = "CREATE DATABASE "+db+" ON PRIMARY " +
"(NAME = " + db + "_Data, " +
"FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL10.SQLEXPRESS\\MSSQL\\DATA\\" + db + "Data.mdf', " +
"SIZE = 100MB, MAXSIZE = 100MB, FILEGROWTH = 10%) " +
"LOG ON (NAME = " + db + "_Log, " +
"FILENAME = N'C:\\Program Files\\Microsoft SQL Server\\MSSQL10.SQLEXPRESS\\MSSQL\\DATA\\" + db + "Log.ldf', " +
"SIZE = 1MB, " +
"MAXSIZE = 5MB, " +
"FILEGROWTH = 10%)";
cmd1 = new SqlCommand(str);
cmd1.Connection = cn1;
cmd1.ExecuteNonQuery();
cn1.Close();
con = "ODBC; Driver={SQL Server};Server='" + dataGridView1.CurrentRow.Cells["Servers"].Value.ToString() + "';Initial Catalog=" + db + ";Persist Security Info=True;User ID= '" + dataGridView1.CurrentRow.Cells["UserNames"].Value.ToString() + "';Password='" + dataGridView1.CurrentRow.Cells["Passwords"].Value.ToString() + "'";
}
catch (Exception Ex) { }
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 tableNames = new List();
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 + "';'Admin';'',[" + tableName + "])");
//cmd.Connection = cn1;
//cmd.ExecuteNonQuery();
OleDbCommand cmd = new OleDbCommand("select * into [" + tableName + "] IN " + con + " FROM [" + tableName + "]", connection);
cmd.ExecuteNonQuery();
connection.Close();
}
} catch (Exception Ex) {
connection.Close();
}
connection.Close();
}
}
} catch (Exception Ex) {}
查询就像这样
select * into [ACTYPE] IN ODBC;
Driver={SQL Server};
Server='PC01';
Initial Catalog=TARR;Persist Security Info=True;
User ID= 'sa';Password='123' FROM [ACTYPE]