SqlBulkCopy创建的行多于DataTable中的行

时间:2016-08-11 15:21:12

标签: c# .net csv datatable sqlbulkcopy

我正在使用SqlBulkCopy从CSV导入大约66k行。 CSV被导入到DataTable中,然后传递给下面的方法。

它似乎无限期地将相同的行(或一旦到达结束时重置)复制到数据库中。调试运行了大约30分钟,创建了接近350k的记录,将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;
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

s.WriteToServer(csvFileData.CreateDataReader());