我目前正在从Excel工作表加载数据,该工作表有一个 Headers 到SQL服务器中的表 . 我在SSIS中使用脚本任务 . 一切都工作正常,除了它没有加载 Headers 后的第一行 . 如果我将行移动到工作表的底部,它会正确加载 . Any ideas?
以下是我使用的代码:
string excelconnectionstring =“Provider = Microsoft.ACE.OLEDB.12.0; Data Source =”fullPath“;扩展属性= \”Excel 12.0; HDR = YES; IMEX = 1 \“”;
OleDbConnection oledbconn = new OleDbConnection(excelconnectionstring); Dts.Variables [“User :: FileLoaded”] . Value = false;
try
{
OleDbCommand oledbcmd = new OleDbCommand(exceldataquery, oledbconn);
oledbconn.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlconnectionstring);
bulkcopy.BatchSize = 1000;
bulkcopy.DestinationTableName = sqltable;
while (dr.Read())
{
bulkcopy.WriteToServer(dr);
}
Dts.Variables["User::FileLoaded"].Value = true;
}
catch (Exception e)
{
MessageBox.Show(e.Data + " " + e.InnerException + " " + e.Message + " " + e.Source);
Dts.Variables["User::FileLoaded"].Value = false;
}
finally
{
oledbconn.Close();
Dts.TaskResult = (int)ScriptResults.Success;
}
1 回答
好的,我通过改变我使用的方法解决了这个问题 . 现在一切都很完美 .
我使用数据适配器,如下所示 . 我仍然不知道为什么以前的代码无法正常工作
OleDbDataAdapter adapter = new OleDbDataAdapter(exceldataquery,oledbconn); adapter.Fill(dataTable中);