【问题标题】:Load multiple files to multiple tables in ssis将多个文件加载到ssis中的多个表中
【发布时间】:2016-11-11 09:04:44
【问题描述】:

我在某个位置有两个文本文件,其中包含列名和数据。

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

为了将这些文本文件中的数据加载到数据库中的各个表中,我使用了脚本任务,但是 ssis 包在执行时给出了运行时错误。

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

我在下面提供必要的屏幕截图和代码:-

变量:

脚本任务代码

[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

}

错误

【问题讨论】:

    标签: sql-server database ssis


    【解决方案1】:

    您可以为文件的两个不同路径创建两个变量并将路径分配给它们。 然后你可以使用 Foreach 循环容器并从他们那里继续。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多