我正在尝试使用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]列包含用作列定界符的逗号(,),因此未正确导入行
有什么建议
一个警告:我不是常规的C#编码器。
但是无论如何,此代码执行以下操作:
它打开一个名为C:\ Input.TXT的文件
它搜索每一行。如果该行有五个以上的逗号,则它将所有多余的逗号从倒数第三个字段中删除(注释)
它将结果写入C:\ Output.TXT-这是您实际需要导入的结果
可以进行许多改进:
请记住,您的软件包将需要对相应文件夹的写权限
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; }