我正在寻求帮助,使用BULK INSERT将.csv文件导入SQL Server,我有一些基本的问题。

问题:

CSV文件的数据可能有,(逗号)之间(Ex: description),那么我如何使导入处理这些数据? 如果客户端从Excel中创建CSV,那么有逗号的数据被括在“”(双引号)[如下例],那么导入如何处理这一点? 我们如何跟踪某些行是否有坏数据,哪些导入被跳过?(导入会跳过不可导入的行)

下面是带标题的CSV示例:

Name,Class,Subject,ExamDate,Mark,Description
Prabhat,4,Math,2/10/2013,25,Test data for prabhat.
Murari,5,Science,2/11/2013,24,"Test data for his's test, where we can test 2nd ROW, Test."
sanjay,4,Science,,25,Test Only.

和SQL语句导入:

BULK INSERT SchoolsTemp
FROM 'C:\CSVData\Schools.csv'
WITH
(
    FIRSTROW = 2,
    FIELDTERMINATOR = ',',  --CSV field delimiter
    ROWTERMINATOR = '\n',   --Use to shift the control to next row
    TABLOCK
)

当前回答

以下是我的解决方法:

只需将您的CSV文件保存为excel中的XLS表(通过这样做,您不必担心分隔符。Excel的电子表格格式将被读取为一个表,并直接导入到SQL表中) 使用SSIS导入文件 在导入管理器中编写一个自定义脚本,以省略/修改您正在寻找的数据。(或者运行一个主脚本来仔细检查要删除的数据)

祝你好运。

其他回答

解决数据中逗号问题的最好、最快和最简单的方法是在将Windows的列表分隔符设置为逗号以外的内容(如管道)后,使用Excel保存一个逗号分隔的文件。这将为您生成一个管道(或其他)分离文件,然后您可以导入该文件。这里描述了这一点。

首先,您需要导入CSV文件到数据表

然后可以使用SQLBulkCopy插入大容量行

using System;
using System.Data;
using System.Data.SqlClient;

namespace SqlBulkInsertExample
{
    class Program
    {
      static void Main(string[] args)
        {
            DataTable prodSalesData = new DataTable("ProductSalesData");

            // Create Column 1: SaleDate
            DataColumn dateColumn = new DataColumn();
            dateColumn.DataType = Type.GetType("System.DateTime");
            dateColumn.ColumnName = "SaleDate";

            // Create Column 2: ProductName
            DataColumn productNameColumn = new DataColumn();
            productNameColumn.ColumnName = "ProductName";

            // Create Column 3: TotalSales
            DataColumn totalSalesColumn = new DataColumn();
            totalSalesColumn.DataType = Type.GetType("System.Int32");
            totalSalesColumn.ColumnName = "TotalSales";

            // Add the columns to the ProductSalesData DataTable
            prodSalesData.Columns.Add(dateColumn);
            prodSalesData.Columns.Add(productNameColumn);
            prodSalesData.Columns.Add(totalSalesColumn);

            // Let's populate the datatable with our stats.
            // You can add as many rows as you want here!

            // Create a new row
            DataRow dailyProductSalesRow = prodSalesData.NewRow();
            dailyProductSalesRow["SaleDate"] = DateTime.Now.Date;
            dailyProductSalesRow["ProductName"] = "Nike";
            dailyProductSalesRow["TotalSales"] = 10;

            // Add the row to the ProductSalesData DataTable
            prodSalesData.Rows.Add(dailyProductSalesRow);

            // Copy the DataTable to SQL Server using SqlBulkCopy
            using (SqlConnection dbConnection = new SqlConnection("Data Source=ProductHost;Initial Catalog=dbProduct;Integrated Security=SSPI;Connection Timeout=60;Min Pool Size=2;Max Pool Size=20;"))
            {
                dbConnection.Open();
                using (SqlBulkCopy s = new SqlBulkCopy(dbConnection))
                {
                    s.DestinationTableName = prodSalesData.TableName;

                    foreach (var column in prodSalesData.Columns)
                        s.ColumnMappings.Add(column.ToString(), column.ToString());

                    s.WriteToServer(prodSalesData);
                }
            }
        }
    }
}

2)如果客户端从excel中创建了csv,那么有数据 逗号被括在"…(双引号)[如下所示 那么导入如何处理这个问题呢?

你应该使用FORMAT = 'CSV', FIELDQUOTE = '"'选项:

BULK INSERT SchoolsTemp
FROM 'C:\CSVData\Schools.csv'
WITH
(
    FORMAT = 'CSV', 
    FIELDQUOTE = '"',
    FIRSTROW = 2,
    FIELDTERMINATOR = ',',  --CSV field delimiter
    ROWTERMINATOR = '\n',   --Use to shift the control to next row
    TABLOCK
)

以下是我的解决方法:

只需将您的CSV文件保存为excel中的XLS表(通过这样做,您不必担心分隔符。Excel的电子表格格式将被读取为一个表,并直接导入到SQL表中) 使用SSIS导入文件 在导入管理器中编写一个自定义脚本,以省略/修改您正在寻找的数据。(或者运行一个主脚本来仔细检查要删除的数据)

祝你好运。

Import the file into Excel by first opening excel, then going to DATA, import from TXT File, choose the csv extension which will preserve 0 prefixed values, and save that column as TEXT because excel will drop the leading 0 otherwise (DO NOT double click to open with Excel if you have numeric data in a field starting with a 0 [zero]). Then just save out as a Tab Delimited Text file. When you are importing into excel you get an option to save as GENERAL, TEXT, etc.. choose TEXT so that quotes in the middle of a string in a field like YourCompany,LLC are preserved also...

BULK INSERT dbo.YourTableName
FROM 'C:\Users\Steve\Downloads\yourfiletoIMPORT.txt'
WITH (
FirstRow = 2, (if skipping a header row)
FIELDTERMINATOR = '\t',
ROWTERMINATOR   = '\n'
)

我希望我可以使用FORMAT和Fieldquote功能,但在我的SSMS版本中似乎不支持