简体   繁体   中英

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:

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

I want to create a dynamic SQL table from this csv like below:

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

Because File Delimiter = ','

Can you please help with what should I include in the c# code to handle this?

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

Dealing with CSV files using string.Replace is just nonsense.

If you can't install 3rd party lib, maybe try this (I didn't tested it): 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 */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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