2016-11-11 128 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循環容器並從它們開始。

相關問題