软件包失败,因为System.Data.SqlClient.SqlException(0x80131904):'s'附近的语法不正确

时间:2019-04-23 16:31:59

标签: c# sql sql-server csv ssis

我的程序包已经好几天了,今天,无论何时我尝试运行它,它都从文件夹中的第一个文件转移了几列,但失败了。

我得到的错误是:

  

System.Data.SqlClient.SqlException(0x80131904):'s'附近的语法不正确。

     

字符串后的右引号
  '','C:\ Users \ svojnovic \ Dropbox \ test \ 2016-08-31 Race 3 Scale Sheet.csv')'。

我在脚本任务中的代码是:

SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)(Dts.Connections["DBConn"].AcquireConnection(Dts.Transaction) as SqlConnection);

// Writing Data of File Into Table
int counter = 0;
string line;
// MessageBox.Show(fileName);

System.IO.StreamReader SourceFile = new System.IO.StreamReader(fileName);

while ((line = SourceFile.ReadLine()) != null)
{
    if (counter > 0)
    {
         string query = "Insert into " + TableName + " Values ('";
         query += line.Replace(FileDelimiter, "','") + "','" + fileName.Replace(SourceFolderPath,"") + "')";

         MessageBox.Show(query.ToString());

         SqlCommand myCommand1 = new SqlCommand(query, myADONETConnection);
         myCommand1.ExecuteNonQuery();
     }

     counter++;
 }

 SourceFile.Close();

 // move the file to archive folder after adding datetime to it
 File.Move(fileName, ArchiveFolder + "\\" + (fileName.Replace(SourceFolderPath, "")).Replace(FileExtension, "") + "_" + datetime + FileExtension);
 Dts.TaskResult = (int)ScriptResults.Success;

1 个答案:

答案 0 :(得分:1)

为什么要在脚本任务中导入平面文件?

可以用不同的方式提出问题:如果要在脚本任务而不是数据流任务中导入平面文件,那么使用SSIS有什么好处?

您可以简单地添加一个数据流任务,并使用平面文件源和OLEDB目标将数据从平面文件导入SQL表。另一方面,这将消除由于串联SQL命令而导致的SQL注入风险。


其他方法

(1)SQL批量插入

(2)SQL OPENROWSET

(3)C#参数化查询


有用的帖子