title: Load multiple files to multiple tables in ssis
body: <p>I have two text files in some location containing column name and data.</p>
  
  <p>The text files are named accordingly two blank tables created in database. </p>
  
  <p>To load the data from those text files to respective tables in database I have used script task,but,the ssis package gave runtime error on execution.</p>
  
  <p>Any workaround or resolution of this situation would be highly appreciated.</p>
  
  <p>I am providing necessary screenshots and code below:-</p>
  
  <p><strong>Variable:</strong>
  <a href="https://i.stack.imgur.com/PKMWM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PKMWM.png" alt="enter image description here"></a></p>
  
  <p><strong>Script Task Code</strong>: </p>
  
  <pre><code>[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
  
  }
  </code></pre>
  
  <p><strong>Error</strong>:</p>
  
  <p><a href="https://i.stack.imgur.com/zOZPd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zOZPd.png" alt="enter image description here"></a></p>