尝试使用C#脚本将logging复制到Excel之前,从csv文件中的行中转义单引号
在SSIS包中,我试图将.CSV文件的内容写入.XLS文件,因为Excel连接pipe理器不能正常工作,但在通过下面的C#脚本尝试完成时遇到问题。
只是澄清一点,我不插入logging到数据库,但到一个EXCEL文件。
任何帮助/方向将不胜感激。 谢谢。
如果我在创build.CSV文件的SQL代码中执行REPLACE函数来删除单引号并再次运行C#代码,那么问题就解决了,但是这个excel文件的pipe理器希望它在那里,我想补救代码,以提供这一点。
我在执行代码时遇到这个错误:
System.Data.OleDb.OleDbException (0x80040E14): Syntax error (missing operator) in query expression ''SAM'S/COSTCO - RETAIL/CONSUMAB')'. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at ST_3c46305a87ab4837b1d2cd33ee47f34c.ScriptMain.Main()
在下面的代码中,我尝试在下面的WHILE循环中的FIRST行之前插入以下内容:
line = line.REPLACE(“'”,“\'”);
这里是我正在使用的C#脚本代码:
try { //Declare Variables string SourceFolderPath = Dts.Variables["User::SourceFolderPath"].Value.ToString(); string DestinationFolderPath = Dts.Variables["User::DestinationFolderPath"].Value.ToString(); string FileExtension = Dts.Variables["User::FileExtension"].Value.ToString(); string FileDelimiter = Dts.Variables["User::FileDelimiter"].Value.ToString(); string CreateTableStatement = ""; string ColumnList = ""; //Reading file names one by one string SourceDirectory = SourceFolderPath; string[] fileEntries = Directory.GetFiles(SourceDirectory, "*" + FileExtension); foreach (string fileName in fileEntries) { // do something with fileName //MessageBox.Show(fileName); //Read first line(Header) and prepare Create Statement for Excel Sheet System.IO.StreamReader file = new System.IO.StreamReader(fileName); string filenameonly = (((fileName.Replace(SourceDirectory, "")).Replace(FileExtension, "")).Replace("\\", "")); CreateTableStatement = (" Create Table [" + filenameonly + "] ([" + file.ReadLine().Replace(FileDelimiter, "] Text,[")) + "] Text)"; file.Close(); //MessageBox.Show(CreateTableStatement.ToString()); //Construct ConnectionString for Excel //string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + DestinationFolderPath + "\\" + filenameonly // + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;\""; string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + DestinationFolderPath + "\\" + filenameonly + ";" + "Extended Properties=\"Excel 8.0;HDR=YES;IMEX=0\""; OleDbConnection Excel_OLE_Con = new OleDbConnection(); OleDbCommand Excel_OLE_Cmd = new OleDbCommand(); //drop Excel file if exists File.Delete(DestinationFolderPath + "\\" + filenameonly + ".xls"); Excel_OLE_Con.ConnectionString = connstring; Excel_OLE_Con.Open(); Excel_OLE_Cmd.Connection = Excel_OLE_Con; //Use OLE DB Connection and Create Excel Sheet Excel_OLE_Cmd.CommandText = CreateTableStatement; Excel_OLE_Cmd.ExecuteNonQuery(); //Writing Data of File to Excel Sheet in Excel File int counter = 0; string line; System.IO.StreamReader SourceFile = new System.IO.StreamReader(fileName); while ((line = SourceFile.ReadLine()) != null) { if (counter == 0) { ColumnList = "[" + line.Replace(FileDelimiter, "],[") + "]"; } else { string query = "Insert into [" + filenameonly + "] (" + ColumnList + ") VALUES('" + line.Replace(FileDelimiter, "','") + "')"; // MessageBox.Show(query.ToString()); var command = query; Excel_OLE_Cmd.CommandText = command; Excel_OLE_Cmd.ExecuteNonQuery(); } counter++; } Excel_OLE_Con.Close(); SourceFile.Close(); Dts.TaskResult = (int)ScriptResults.Success; } } catch (Exception exception) { // Create Log File for Errors using (StreamWriter sw = File.CreateText(Dts.Variables["User::DestinationFolderPath"].Value.ToString() + "\\" + "ErrorLog_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log")) { sw.WriteLine(exception.ToString()); Dts.TaskResult = (int)ScriptResults.Failure; } } Dts.TaskResult = (int)ScriptResults.Success;
line = line.REPLACE("'", "''");
除非我完全错误,否则这应该适合你。