在SSIS中添加页眉和页脚行平面文件

时间:2011-12-11 20:44:33

标签: c# sql-server-2008 ssis

我有一个SSIS包,它将查询中的数据导出到一个平面文件中,该文件将用于导入数据仓库。我的一个要求是添加一个包含当前日期的标题行,以及一个包含总行数的页脚行。

我想在一个脚本组件或任务中理想地使用C#来实现这一点,以便在包中保持整洁。在编写代码时,我是一个菜鸟。如何才能做到这一点?我在网上环顾四周,但似乎找不到足够接近我想要的东西。

3 个答案:

答案 0 :(得分:1)

以下是您可以用于脚本任务的代码,该脚本任务允许您输出带有页眉和页脚的CSV:

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace ST_80294de8b8dd4779a54f707270089f8c.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

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

        public void Main()
        {
            int ErrorFlag = 0;

            // Try-Catch block 
            try
            {
                int RowCount = 0;
                bool fireAgain = true;
                string SQLCommandText = "SELECT ColumnA = 1, ColumnB = 'A' UNION SELECT ColumnA = 2, ColumnB = 'B' UNION SELECT ColumnA = 3, ColumnB = 'C';";

                SqlConnection SQLConnection = new SqlConnection("Data Source=LocalHost;Initial Catalog=master;Integrated Security=SSPI;Application Name=SSIS-My Package Name;Connect Timeout=600");
                SqlCommand SQLCommand = new SqlCommand(SQLCommandText, SQLConnection);
                SQLCommand.CommandTimeout = 60 * 60;
                SqlDataAdapter SQLDataAdapter = new SqlDataAdapter(SQLCommand);
                DataTable dt = new DataTable();
                SQLDataAdapter.Fill(dt);
                SQLConnection.Close();
                RowCount = dt.Rows.Count;

                Dts.Events.FireInformation(0, "DataTable Rows", RowCount.ToString(), "", 0, ref fireAgain);

                StreamWriter sw = new StreamWriter("C:\\Test.csv", false);

                // Write the header.
                sw.Write("Today's date is " + DateTime.Now.ToLongDateString());

                // Write the column headers.
                sw.Write(sw.NewLine);
                int iColCount = dt.Columns.Count;
                for (int i = 0; i < iColCount; i++)
                {
                    sw.Write(dt.Columns[i]);
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }

                // Write the details.
                sw.Write(sw.NewLine);
                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < iColCount; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            sw.Write(dr[i].ToString());
                        }
                        if (i < iColCount - 1)
                        {
                            sw.Write(",");
                        }
                    }
                    sw.Write(sw.NewLine);
                }

                // Write the footer.
                sw.Write("Row count: " + RowCount.ToString());

                sw.Close();
            }

            catch (SqlException e)
            {
                Dts.Events.FireError(0, "SqlException", e.Message, "", 0);
                ErrorFlag = 1;
            }

            catch (IOException e)
            {
                Dts.Events.FireError(0, "IOException", e.Message, "", 0);
                ErrorFlag = 1;
            }

            catch (Exception e)
            {
                Dts.Events.FireError(0, "Exception", e.Message, "", 0);
                ErrorFlag = 1;
            }

            // Return results. 
            if (ErrorFlag == 0)
            {
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            else
            {
                Dts.TaskResult = (int)ScriptResults.Failure;
            } 

        }
    }
}

你也可以在不诉诸C#的情况下做到这一点,但这会有点难看:

  1. 变量1:用于指定数据流2中行数的Int变量。

  2. 变量2:带有生成SQL命令的表达式的字符串变量。如果变量1名为RowCount,那么这里是一组示例代码:

    &#34; SELECT ColumnA =&#39;&#34; +(DT_WSTR,1252)(@ [User :: RowCount])+&#34;&#39;,ColumnB = NULL&#34;

  3. 数据流1:执行SQL命令以生成文件的标头并输出到平面文件目的地。设置文件中的&#34;覆盖数据&#34;真实。

  4. 数据流2:执行SQL命令以生成平面文件的详细信息。设置文件中的&#34;覆盖数据&#34;为假。包括行计数转换并将值分配给变量1.

  5. 数据流3:执行SQL命令以生成平面文件的页脚。源应该&#34;设置命令来自变量&#34;它应该执行变量2.设置&#34;覆盖文件中的数据&#34;为假。

答案 1 :(得分:1)

答案 2 :(得分:0)

这是我最终想出来的!这是我能找到的最干净,最简单的方法。它基本上只是构建标题和尾部行,然后附加到数据集。一旦你完成它,看起来很简单!它需要一些C#的知识,但是它比在SQL中尝试它更值得。

Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.

using System;
using System.Text;
using System.IO;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;

namespace ST_db04adc927b941d19b3817996ff885c2.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

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

        /*
        The execution engine calls this method when the task executes.
        To access the object model, use the Dts property. Connections, variables, events,
        and logging features are available as members of the Dts property as shown in the following examples.

        To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
        To post a log entry, call Dts.Log("This is my log text", 999, null);
        To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);

        To use the connections collection use something like the following:
        ConnectionManager cm = Dts.Connections.Add("OLEDB");
        cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";

        Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.

        To open Help, press F1.
    */

        public void Main()
        {
            const string dirPath = @"C:\SSIS\Dev\";

            DateTime minusoneweek = DateTime.Today.AddDays(-7);

            DateTime minusoneday = DateTime.Today.AddDays(-1);

            var headerRecord = ("0|" + DateTime.Today.ToString("ddMMyyyy") + "|" + Dts.Variables["LastSequenceNumber"].Value + "|" 
                + Dts.Variables["FileName"].Value) + "|" + minusoneweek.ToString("ddMMyyyy") + "|" + minusoneday.ToString("ddMMyyyy");

            var fileBody = AddHeaderAndFooter.GetFileText(dirPath + "blank.txt");

            var trailerRecord = "9|" + AddHeaderAndFooter.CountRecords(dirPath + "blank.txt").ToString();

            var outPutData = headerRecord + "\r\n" + fileBody + trailerRecord + "\r\n";

            AddHeaderAndFooter.WriteToFile(dirPath + "blank.txt", outPutData);

        }
    }

    public static class AddHeaderAndFooter
    {
        public static int CountRecords(string filePath)
        {

            return (File.ReadAllLines(filePath).Length + 2);  

        }

        public static string GetFileText(string filePath)
        {
            var sr = new StreamReader(filePath, Encoding.Default);

            var recs = sr.ReadToEnd();

            sr.Close();

            return recs;
        }

        public static void WriteToFile(string filePath, string fileText)
        {

            var sw = new StreamWriter(filePath, false);

            sw.Write(fileText, Encoding.ASCII);

            sw.Close();

        }
    }
}