我的目的是将Excel数据复制到SQL Server Compact Edition中。
我已经从 here 读取了SQL Server Compact和SQL Server之间的区别,但是找不到它。
另外,知道SqlBulkCopy的构造函数接受SqlConnection的参数而不是SqlCeConnection。
这是我的代码:
public void importDataFromExcel(string excelFilePath)
{
string sSQLTable = "Inflations";
string myExcelDataQuery = "Select CUSIP,FM,TNT,CPI_INDEX from [Sheet1$]";
string sExcelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";Extended Properties=" + "\"Excel 8.0;HDR=YES;\"";
string sSqlConnectionString = "Data Source=C:\\Users\\abc\\Documents\\datafile.sdf;Password=12345;";
OleDbConnection OleDbConn = new OleDbConnection(sExcelConnectionString);
OleDbCommand OleDbCmd = new OleDbCommand(myExcelDataQuery, OleDbConn);
OleDbConn.Open();
OleDbDataReader dr = OleDbCmd.ExecuteReader();
SqlBulkCopy bulkCopy = new SqlBulkCopy(sSqlConnectionString);
bulkCopy.DestinationTableName = sSQLTable;
while (dr.Read())
{
bulkCopy.WriteToServer(dr);
}
OleDbConn.Close();
}
答案 0 :(得分:2)
您可以使用我的SqlCeBulkCopy库,可通过NuGet获取SQL CE 4.0,也可以在此处获得:https://sqlcebulkcopy.codeplex.com/
答案 1 :(得分:0)