简体   繁体   English

如何编写 C# 脚本来处理 SSIS 中 csv 字段中的双引号

[英]How to write C# script to handle double quotes in csv fields in SSIS

My requirement is that I am getting a CSV source file like this:我的要求是我得到一个 CSV 源文件,如下所示:

ID,[%age],[name]
75,8,"Ander,Pat"

I want to create a dynamic SQL table from this csv like below:我想从此 csv 创建一个动态 SQL 表,如下所示:

ID,[%age],[name]
75,8,Ander,Pat (this should come in a single column)

The problem is that my C# code is reading the [name] as two different columns ie Ander and Pat问题是我的 C# 代码将 [name] 读取为两个不同的列,即 Ander 和 Pat

Because File Delimiter = ','因为文件分隔符 = ','

Can you please help with what should I include in the c# code to handle this?你能帮我在 c# 代码中包含什么来处理这个问题吗?

EDIT: Here is my code:编辑:这是我的代码:

                string TableName = "";
                int counter = 0;
                string line;
                string ColumnList = "";
        

                System.IO.StreamReader SourceFile =
                new System.IO.StreamReader(fileName);
                while ((line = SourceFile.ReadLine()) != null)
                {
                    if (counter == 0)
                    {
                        ColumnList = "[" + line.Replace(FileDelimiter, "],[") + "]";
                        TableName = (((fileName.Replace(SourceFolderPath, "")).Replace(FileExtension, "")).Replace("\\", ""));
                        string CreateTableStatement = "IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[" + SchemaName + "].";
                        CreateTableStatement += "[" + TableName + "]')";
                        CreateTableStatement += " AND type in (N'U'))DROP TABLE [" + SchemaName + "].";
                        CreateTableStatement += "[" + TableName + "]  Create Table " + SchemaName + ".[" + TableName + "]";
                        CreateTableStatement += "([" + line.Replace(FileDelimiter, "] " + ColumnsDataType + ",[") + "] " + ColumnsDataType + ")";
                        SqlCommand CreateTableCmd = new SqlCommand(CreateTableStatement, myADONETConnection);
                        CreateTableCmd.ExecuteNonQuery();

                        

                    }
                    else
                    {
                        string query = "Insert into " + SchemaName + ".[" + TableName + "] (" + ColumnList + ") ";
                        query += "VALUES('" + line.Replace(FileDelimiter, "','") + "')";

You need to use some library, like CsvHelper您需要使用一些库,例如CsvHelper

Dealing with CSV files using string.Replace is just nonsense.使用 string.Replace 处理 CSV 文件只是一派胡言。

If you can't install 3rd party lib, maybe try this (I didn't tested it): StackOverflow Link如果您无法安装 3rd 方库,可以试试这个(我没有测试过): StackOverflow Link

var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(file);
parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
parser.SetDelimiters(new string[] { "," });

while (!parser.EndOfData)
{
    string[] row = parser.ReadFields();
    /* do something */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM