从 Excel 到 Sql Server 2008 的脚本任务批量复制。缺少 header 之后的第一行
script task bulkcopy from Excel to Sql Server 2008. Missing first row after the header
我目前正在从 Excel 工作表中加载数据,该工作表具有 header 到 SQL 服务器中的 table。我在 SSIS 中使用脚本任务。一切正常,只是它不加载 header 之后的第一行。如果我将该行移动到工作表的底部,它会正确加载。
以下是我使用的代码:
string excelconnectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullPath + ";Extended Properties=\"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;
}
好的,所以我通过改变我使用的方法解决了这个问题。现在一切正常。
我使用数据适配器代替,如下所示。我仍然不知道为什么以前的代码不能正常工作
OleDbDataAdapter 适配器 = new OleDbDataAdapter(exceldataquery, oledbconn);
adapter.Fill(数据表);
SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlconnectionstring);
bulkcopy.BatchSize = 1000;
bulkcopy.DestinationTableName = sqltable;
bulkcopy.WriteToServer(dataTable);
我目前正在从 Excel 工作表中加载数据,该工作表具有 header 到 SQL 服务器中的 table。我在 SSIS 中使用脚本任务。一切正常,只是它不加载 header 之后的第一行。如果我将该行移动到工作表的底部,它会正确加载。
以下是我使用的代码:
string excelconnectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullPath + ";Extended Properties=\"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;
}
好的,所以我通过改变我使用的方法解决了这个问题。现在一切正常。
我使用数据适配器代替,如下所示。我仍然不知道为什么以前的代码不能正常工作
OleDbDataAdapter 适配器 = new OleDbDataAdapter(exceldataquery, oledbconn); adapter.Fill(数据表);
SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlconnectionstring);
bulkcopy.BatchSize = 1000;
bulkcopy.DestinationTableName = sqltable;
bulkcopy.WriteToServer(dataTable);