我使用SqlBulkCopy從CSV導入大約66k行。將CSV導入到DataTable中,然後傳遞到下面的以下方法。SqlBulkCopy創建比DataTable更多的行
似乎無限期地將相同的行復制到數據庫中(或者一旦到達結尾就重置)。調試運行了大約30分鐘,並創建了近5萬個記錄,每次複製DataTable中的每一行大約5次。
我可以確認DataTable中的行數是正確的,所以我必須缺少一些東西,任何想法?
注意:我添加了s.BatchSize,希望它只會導入不是這種情況的數量。
/*
* StoreImportedData()
* This method will store all of the imported data from the datatable to the SQL server database.
*
*
*/
private Boolean StoreImportedData(DataTable csvFileData)
{
try
{
using (SqlConnection dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
dbConnection.Open();
using (SqlBulkCopy s = new SqlBulkCopy(dbConnection))
{
s.DestinationTableName = "Staging";
// Amount of rows to issue in the batch: (currently value is around 60k)
s.BatchSize = csvFileData.Rows.Count;
s.ColumnMappings.Add("remark", "ReportDate");
s.ColumnMappings.Add("ipAddress", "IPAddress");
s.ColumnMappings.Add("hostName", "HostName");
s.ColumnMappings.Add("macAddress", "MacAddress");
s.ColumnMappings.Add("deviceName", "PrinterName");
s.ColumnMappings.Add("comment", "CenterCode");
s.ColumnMappings.Add("userName", "CustomerCode");
s.ColumnMappings.Add("userDisplayName", "CustomerName");
s.ColumnMappings.Add("printerBlackTotal", "TPrintBw");
s.ColumnMappings.Add("printerColorTotal", "TPrintCol");
s.ColumnMappings.Add("copyBlackTotal", "TCopyBw");
s.ColumnMappings.Add("copyColorTotal", "TCopyCol");
s.ColumnMappings.Add("scanTotal", "TScan");
s.ColumnMappings.Add("faxBlackTotal", "TFaxBw");
s.WriteToServer(csvFileData);
}
}
}
catch (Exception e)
{
throw new Exception("An error has occured with the import SQL operation: " + e.Message);
}
return true;
}
}
你對臨時表的唯一索引或主鍵? –
是的,有一個自動增加的主鍵(Id) – mauzilla
我的猜測是在表上添加一個唯一索引以防止多次插入,主鍵是自動增量不會有幫助。 –