Import CSV File Error : Column Value containing column delimiter

后端 未结 3 879
花落未央
花落未央 2020-12-20 09:40

I am trying to Import a Csv File into SQL SERVER using SSIS

Here\'s an example how data looks like

Student_Name,Student_DOB,Student_ID,Student_Notes         


        
相关标签:
3条回答
  • 2020-12-20 10:11

    If import CSV file is not a routine

    1. Import CSV file in Excel
    2. Search error rows with Excel rows filter and rewrite them
    3. Save Excel file in TXT Tab delimited
    4. Import TXT file with SSIS Else make a script that search comma in the Student Notes column range
    0 讨论(0)
  • 2020-12-20 10:20

    In The Flat File Connection Manager. Make the File as only one column (DT_STR 8000)

    Just add a script Component in the dataflowtask and Add Output Columns (Same as Example Shown)

    in The script component split each row using the following Code:

    \\Student_Name,Student_DOB,Student_ID,Student_Notes,Student_Gender,Student_Mother_Name
    
    Dim strCells() as string = Row.Column0.Split(CChar(","))
    
    Row.StudentName = strCells(0)
    Row.StudentDOB = strCells(1)
    Row.StudentID = strCells(2)
    Row.StudentMother = strCells(strCells.Length - 1)
    Row.StudentGender = strCells(strCells.Length - 2)
    
    Dim strNotes as String = String.Empty
    
    For int I = 3 To strCells.Length - 3
    
    strNotes &= strCells(I)
    
    Next
    
    Row.StudentNotes = strNotes
    

    it worked fine for me

    0 讨论(0)
  • 2020-12-20 10:37

    A word of warning: I'm not a regular C# coder.

    But anyway this code does the following:

    It opens a file called C:\Input.TXT

    It searches each line. If the line has more than 5 commas, it takes all the extra commas out of the third last field (notes)

    It writes the result to C:\Output.TXT - that's the one you need to actually import

    There are many improvements that could be made:

    • Get file paths from connection managers
    • Error handling
    • An experienced C# programmer could probably do this in hlaf the code

    Keep in mind your package will need write access to the appropriate folder

    public void Main()
    {
        // Search the file and remove extra commas from the third last field
        // Extended from code at
        // http://stackoverflow.com/questions/1915632/open-a-file-and-replace-strings-in-c-sharp
        // Nick McDermaid        
    
        string sInputLine;
        string sOutputLine;
        string sDelimiter = ",";
        String[] sData;
        int iIndex;
    
        // open the file for read
        using (System.IO.FileStream inputStream = File.OpenRead("C:\\Input.txt"))
        {
            using (StreamReader inputReader = new StreamReader(inputStream))
            {
                // open the output file
                using (StreamWriter outputWriter = File.AppendText("C:\\Output.txt"))
                {
                    // Read each line
                    while (null != (sInputLine = inputReader.ReadLine()))
                    {
                        // Grab each field out
                        sData = sInputLine.Split(sDelimiter[0]);
                        if (sData.Length <= 6)
                        {
                            // 6 or less fields - just echo it out
                            sOutputLine = sInputLine;
                        }
                        else
                        {
                            // line has more than 6 pieces 
                            // We assume all of the extra commas are in the notes field                                
    
                            // Put the first three fields together
                            sOutputLine =
                                sData[0] + sDelimiter +
                                sData[1] + sDelimiter +
                                sData[2] + sDelimiter;
    
                            // Put the middle notes fields together, excluding the delimiter
                            for (iIndex=3; iIndex <= sData.Length - 3; iIndex++)
                            {
                                sOutputLine = sOutputLine + sData[iIndex] + " ";
                            }
    
                            // Tack on the last two fields
                            sOutputLine = sOutputLine +
                                sDelimiter + sData[sData.Length - 2] +
                                sDelimiter + sData[sData.Length - 1];
    
    
                        }
    
                        // We've evaulted the correct line now write it out
                        outputWriter.WriteLine(sOutputLine);
                    }
                }
            }
        }
    
    
        Dts.TaskResult = (int)Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success;
    }
    
    0 讨论(0)
提交回复
热议问题