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

348 Views Asked by At

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 i.e 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, "','") + "')";
1

There are 1 best solutions below

1
On

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 */
}