Sql 导入CSV文件错误:列值包含列分隔符

Sql 导入CSV文件错误:列值包含列分隔符,sql,sql-server,csv,ssis,delimiter,Sql,Sql Server,Csv,Ssis,Delimiter,我正在尝试使用SSIS将Csv文件导入SQL SERVER 下面是一个数据的外观示例 Student_Name,Student_DOB,Student_ID,Student_Notes,Student_Gender,Student_Mother_Name Joseph Jade,2005-01-01,1,Good listener,Male,Amy Amy Jade,2006-01-01,1,Good in science,Female,Amy .... Csv列不包含文本限定符(引号) 我使

我正在尝试使用SSIS将Csv文件导入SQL SERVER

下面是一个数据的外观示例

Student_Name,Student_DOB,Student_ID,Student_Notes,Student_Gender,Student_Mother_Name
Joseph Jade,2005-01-01,1,Good listener,Male,Amy
Amy Jade,2006-01-01,1,Good in science,Female,Amy
....
Csv列不包含文本限定符(引号)

我使用SSIS创建了一个简单的包,将其导入SQL,但有时SQL中的数据如下所示

Student_Name    Student_DOB Student_ID  Student_Notes   Student_Gender  Student_Mother_Name
Ali Jade    2004-01-01  1   Good listener   Bad in science  Male,Lisa
原因是有时[Student_Notes]列包含用作列分隔符的逗号(,),因此无法正确导入该行


如果导入CSV文件不是例行程序,有什么建议吗

  • 在Excel中导入CSV文件
  • 使用Excel行筛选器搜索错误行并重写它们
  • 以TXT制表符分隔的格式保存Excel文件
  • 使用SSIS导入TXT文件 否则,制作一个脚本,在学生笔记列范围内搜索逗号

  • 如果导入CSV文件不是例行程序

  • 在Excel中导入CSV文件
  • 使用Excel行筛选器搜索错误行并重写它们
  • 以TXT制表符分隔的格式保存Excel文件
  • 使用SSIS导入TXT文件 否则,制作一个脚本,在学生笔记列范围内搜索逗号

  • 警告一句:我不是一个普通的C#coder

    但无论如何,此代码执行以下操作:

    它会打开一个名为C:\Input.TXT的文件

    它搜索每一行。如果该行有5个以上的逗号,则从最后第三个字段(备注)中去掉所有多余的逗号

    它将结果写入C:\Output.TXT,这就是您需要实际导入的结果

    可以进行许多改进:

    • 从连接管理器获取文件路径
    • 错误处理
    • 一个有经验的C#程序员可能可以在hlaf代码中实现这一点
    请记住,您的包将需要对相应文件夹的写访问权限

    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;
    }
    
    public void Main()
    {
    //搜索文件并从最后三个字段中删除多余的逗号
    //从代码扩展到
    // http://stackoverflow.com/questions/1915632/open-a-file-and-replace-strings-in-c-sharp
    //尼克·麦克德迈德
    弦线;
    字符串sOutputLine;
    字符串sDelimiter=“,”;
    字符串[]sData;
    国际指数;
    //打开文件进行读取
    使用(System.IO.FileStream inputStream=File.OpenRead(“C:\\Input.txt”))
    {
    使用(StreamReader inputReader=新StreamReader(inputStream))
    {
    //打开输出文件
    使用(StreamWriter outputWriter=File.AppendText(“C:\\Output.txt”))
    {
    //读每一行
    while(null!=(sInputLine=inputReader.ReadLine())
    {
    //抓住每一块地
    sData=sInputLine.Split(sDelimiter[0]);
    
    如果(sData.Length警告一句:我不是一个普通的C#编码器

    但无论如何,此代码执行以下操作:

    它会打开一个名为C:\Input.TXT的文件

    它搜索每一行。如果该行有5个以上的逗号,则从最后第三个字段(注释)中取出所有多余的逗号

    它将结果写入C:\Output.TXT,这就是您需要实际导入的结果

    可以进行许多改进:

    • 从连接管理器获取文件路径
    • 错误处理
    • 一个有经验的C#程序员可能可以在hlaf代码中实现这一点
    请记住,您的包将需要对相应文件夹的写访问权限

    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;
    }
    
    public void Main()
    {
    //搜索文件并从最后三个字段中删除多余的逗号
    //从代码扩展到
    // http://stackoverflow.com/questions/1915632/open-a-file-and-replace-strings-in-c-sharp
    //尼克·麦克德迈德
    弦线;
    字符串sOutputLine;
    字符串sDelimiter=“,”;
    字符串[]sData;
    国际指数;
    //打开文件进行读取
    使用(System.IO.FileStream inputStream=File.OpenRead(“C:\\Input.txt”))
    {
    使用(StreamReader inputReader=新StreamReader(inputStream))
    {
    //打开输出文件
    使用(StreamWriter outputWriter=File.AppendText(“C:\\Output.txt”))
    {
    //读每一行
    while(null!=(sInputLine=inputReader.ReadLine())
    {
    //抓住每一块地
    sData=sInputLine.Split(sDelimiter[0]);
    
    if(sData.Length在平面文件连接管理器中。使文件仅为一列(DT_STR 8000)

    只需在dataflowtask中添加脚本组件并添加输出列(与所示示例相同)

    在脚本组件中,使用以下代码拆分每行:

    \\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
    

    在平面文件连接管理器中,它对我来说工作得很好

    只需在dataflowtask中添加脚本组件并添加输出列(与所示示例相同)

    在脚本组件中,使用以下代码拆分每行:

    \\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
    

    这对我来说很好

    更改列分隔符,将列分隔符放在列数据中是不好的做法如何操作…我不是创建这些csv文件的ont。也许你可以用某种编程语言创建一个程序,以便逐行读取这些文件,并根据需要处理数据。你可以自动确定哪个是他有冒犯性的逗号吗?如果是这样,你可以编写一个脚本来清理它。解决这个问题的一种方法似乎是计算每行的逗号数,如果有太多,从右边开始删除。你能手动将这个算法应用到你的数据中,看看它是否正确。如果是这样,我可以帮助你编写一个脚本,在你死之前清理它nd.我会给你一些代码,但给你一个建议:永远不要说'code plz',这有点烦人。更改列分隔符,在列数据中使用列分隔符是不好的做法如何做…我不是创建这些csv文件的ont也许你可以用某种编程语言创建一个程序,所以你可以逐行阅读这些文件,然后根据需要处理数据