簡體   English   中英

在SSIS中添加頁眉和頁腳行平面文件

[英]Add header and footer row flat file in SSIS

我有一個SSIS包,它將查詢中的數據導出到一個平面文件中,該文件將用於導入數據倉庫。 我的一個要求是添加一個包含當前日期的標題行,以及一個包含總行數的頁腳行。

我想在一個腳本組件或任務中理想地執行此操作,使用C#來實現包中的整潔。 在編寫代碼時,我是一個菜鳥。 如何才能做到這一點? 我在網上環顧四周,但似乎找不到足夠接近我想要的東西。

以下是可用於腳本任務的代碼,該腳本任務允許您輸出帶有頁眉和頁腳的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,那么這里是一組示例代碼:

    “SELECT ColumnA ='”+(DT_WSTR,1252)(@ [User :: RowCount])+“',ColumnB = NULL”

  3. 數據流1:執行SQL命令以生成文件的標頭並輸出到平面文件目的地。 將“覆蓋文件中的數據”設置為true。

  4. 數據流2:執行SQL命令以生成平面文件的詳細信息。 將“覆蓋文件中的數據”設置為false。 包括行計數轉換並將值分配給變量1。

  5. 數據流3:執行SQL命令以生成平面文件的頁腳。 源應該“從變量設置命令”,它應該執行變量2.將“覆蓋文件中的數據”設置為false。

這就是我最終想出來的! 這是我能找到的最干凈,最簡單的方法。 它基本上只是構建標題和尾部行,然后附加到數據集。 一旦你完成它,看起來很簡單! 它需要一些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();

        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM