2016-11-11 23 views
0

ある場所に2つのテキストファイルがあり、列名とデータが含まれています。複数のファイルを複数のテーブルに読み込みます

テキストファイルは、データベースに作成された2つの空白のテーブルに応じて名前が付けられます。

これらのテキストファイルのデータをデータベースのそれぞれのテーブルにロードするにはスクリプトタスクを使用しましたが、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

ファイルの2つの異なるパスに対して2つの変数を作成し、パスを割り当てることができます。 その後、Foreachループコンテナを使用して、そのループコンテナから先に進むことができます。

関連する問題