2016-11-03 165 views
0

我试图导入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 

的原因是somtimes [Student_Notes]列包含逗号(,),其被用作列分隔符,从而将行没有被正确

进口任何建议

+0

更改列分隔符,这是不好的做法,有列数据中列分隔符 – DanielVorph

+0

怎么办呢?我不是谁创造了这些CSV文件的ONT – Hadi

+0

也许 您可以使用某种编程语言创建程序,以便您可以逐行读取这些文件,并根据需要处理 数据 – DanielVorph

回答

1

警告的词搜索逗号:我不是一个常规的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; 
} 
+0

感谢您的帮助 – Hadi

+0

不用担心好运 –

0

如果导入CSV文件不是常规

分隔在Excel
  1. 导入CSV文件
  2. 搜索与Excel行过滤错误行并重写它们
  3. 保存Excel文件中的TXT标签
  4. 与SSIS 导入TXT文件,否则让一个脚本,在学生Notes列范围
+0

请解释第二步。当处理数百行时! – Hadi

+0

对最后一列desc进行排序以识别错误的行。然后,您可以使用Excel仅对错误的行进行一些数据清理(附加单元格等)。显然,只有学生笔记单元中有一个逗号时,此解决方法才有效。另一种解决方法是禁止在web应用文本字段中输入逗号 – Brontomania

0

写访问平面文件连接管理器。使文件,因为只有一列(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 

它的工作对我罚款

相关问题