csv文件批量插入sql server

时间:2014-05-30 13:51:04

标签: c# sql-server csv lumenworks

尝试使用批量插入将.csv文件插入SQL Server时出现问题。

下面的Csv文件示例行:

16777216,16777471,AU,AUSTRALIA,APNIC DEBOGON PROJECT
16777472,16778239,CN,CHINA,CHINANET FUJIAN PROVINCE NETWORK
16778240,16779263,AU,AUSTRALIA,LEVEL 5 530 COLLINS STREET

这是我的代码:

CsvReader csv = new CsvReader(new StreamReader(fileName), false, ',', '\0', '\0', '\0', LumenWorks.Framework.IO.Csv.ValueTrimmingOptions.All);

csv.DefaultParseErrorAction = ParseErrorAction.AdvanceToNextLine;
csv.MissingFieldAction = MissingFieldAction.ReplaceByNull;

copy = new SqlBulkCopy(conn, SqlBulkCopyOptions.KeepIdentity, transaction);
copy.DestinationTableName = "Blabla";

var mapping1 = new SqlBulkCopyColumnMapping(0, "ipFrom");
copy.ColumnMappings.Add(mapping1);

var mapping2 = new SqlBulkCopyColumnMapping(1, "ipTo");
copy.ColumnMappings.Add(mapping2);

var mapping3 = new SqlBulkCopyColumnMapping(2, "countryCode");
copy.ColumnMappings.Add(mapping3);

var mapping4 = new SqlBulkCopyColumnMapping(3, "countryName");
copy.ColumnMappings.Add(mapping4);

var mapping5 = new SqlBulkCopyColumnMapping(4, "isp");
copy.ColumnMappings.Add(mapping5);


copy.WriteToServer(csv);
transaction.Commit();

结果表是:

  

“16777216 16777471 AU AUSTRALIA APNIC DEBOGON PROJECT”“16777472 16778239 CN CHINA CHINANET FUJIAN PRONETCE NETWORK”“16778240 16779263 AU AUSTRALIA LEVEL 5 530 COLLINS STREET”

我无法逐行处理此文件,因为文件大约有1亿行

如何在结尾处丢弃并开始双引号?

2 个答案:

答案 0 :(得分:0)

您可以使用此代码:

  public bool CSVFileRead(string fullPathWithFileName, string fileNameModified, string tableName)
    {
        SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["dbConnectionString"]);
        string filepath = fullPathWithFileName;
        StreamReader sr = new StreamReader(filepath);
        string line = sr.ReadLine();
        string[] value = line.Split(',');
        DataTable dt = new DataTable();
        DataRow row;
        foreach (string dc in value)
        {
            dt.Columns.Add(new DataColumn(dc));
        }
        while (!sr.EndOfStream)
        {
            //string[] stud = sr.ReadLine().Split(',');
            //for (int i = 0; i < stud.Length; i++)
            //{
            //    stud[i] = stud[i].Replace("\"", "");
            //}
            //value = stud;
            value = sr.ReadLine().Split(',');
            if (value.Length == dt.Columns.Count)
            {
                row = dt.NewRow();
                row.ItemArray = value;
                dt.Rows.Add(row);
            }
        }
        SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
        bc.DestinationTableName = tableName;
        bc.BatchSize = dt.Rows.Count;
        con.Open();
        bc.WriteToServer(dt);
        bc.Close();
        con.Close();

        return true;
    }

答案 1 :(得分:0)

您可以使用此代码,但MS Office需要:

  private void ImportCSV(string filePath = @"E:\nucc_taxonomy_140.csv", string tableName = "TempTaxonomyCodes")
    {
        string tempPath = System.IO.Path.GetDirectoryName(filePath);
        string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + tempPath + @"\;Extensions=asc,csv,tab,txt";
        OdbcConnection conn = new OdbcConnection(strConn);
        OdbcDataAdapter da = new OdbcDataAdapter("Select * from " + System.IO.Path.GetFileName(filePath), conn);
        DataTable dt = new DataTable();
        da.Fill(dt);

        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(ConfigurationSettings.AppSettings["dbConnectionString"]))
        {
            bulkCopy.DestinationTableName = tableName;
            bulkCopy.BatchSize = 50;
            bulkCopy.WriteToServer(dt);
        }

    }