2016-11-11 89 views
0

我在包含列名和数据的某个位置有两个文本文件。将多个文件加载到ssis中的多个表中

文本文件相应地命名为两个在数据库中创建的空白表。

要从这些文本文件中的数据加载到数据库中的相应表我已经使用脚本任务,但是,ssis软件包给运行时执行错误。

任何解决方法或这种情况下的解决方案将不胜感激。

我提供必要的截图和代码如下: -

变量: enter image description here

脚本任务代码

[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute] 
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase 
{ 

    public void Main() 
    { 
     // TODO: Add your code here 

     SqlConnection myADONETConnection = new SqlConnection(); 
     myADONETConnection = (SqlConnection)(Dts.Connections["ADO_TestDB"].AcquireConnection(Dts.Transaction) as SqlConnection); 
     // MessageBox.Show(myADONETConnection.ConnectionString, "ADO_TestDB"); 


     //Reading file names one by one 
     string SourceDirectory = Dts.Variables["User::VarFolderPath"].Value.ToString(); 
     // TODO: Add your code here 
     string[] fileEntries = Directory.GetFiles(SourceDirectory); 
     foreach (string fileName in fileEntries) 
     { 
      // do something with fileName 
      // MessageBox.Show(fileName); 
      string columname = ""; 


      //Reading first line of each file and assign to variable 
      System.IO.StreamReader file2 = 
      new System.IO.StreamReader(fileName); 

      string filenameonly = (((fileName.Replace(SourceDirectory, "")).Replace(".txt", "")).Replace("\\", "")); 

      file2.Close(); 

      //Writing Data of File Into Table 
      int counter = 0; 
      string line; 

      System.IO.StreamReader SourceFile = 
      new System.IO.StreamReader(fileName); 
      while ((line = SourceFile.ReadLine()) != null) 
      { 

       if (counter == 0) 
       { 
        columname = line.ToString(); 

       } 

       else 
       { 

        string query = "Insert into dbo." + filenameonly + "(" + columname + ") VALUES('" + line.Replace(",", "','") + "')"; 
        //MessageBox.Show(query.ToString()); 
        SqlCommand myCommand1 = new SqlCommand(query, myADONETConnection); 
        myCommand1.ExecuteNonQuery(); 
       } 
       counter++; 
      } 
      SourceFile.Close(); 
     } 

     Dts.TaskResult = (int)ScriptResults.Success; 
    } 

    #region ScriptResults declaration 

    enum ScriptResults 
    { 
     Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, 
     Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure 
    }; 
    #endregion 

} 

错误

enter image description here

回答

0

您可以为文件的两个不同路径创建两个变量并为其分配路径。 然后你可以使用Foreach循环容器并从它们开始。

相关问题