Добавить плоский файл заголовка и строки нижнего колонтитула в SSIS - PullRequest
0 голосов
/ 12 декабря 2011

У меня есть пакет служб SSIS, который экспортирует данные из запроса в плоский файл, который будет использоваться для импорта в хранилище данных.Одним из моих требований является добавление строки заголовка с текущей датой и строки нижнего колонтитула с общим количеством строк.

В идеале я хотел бы сделать это в одном компоненте скрипта или задаче, используя C # для аккуратности в пакете,Я нуб, когда дело доходит до написания кода.Как это может быть сделано?Я огляделся в сети, но не могу найти что-то достаточно близкое к тому, что я хочу.

Ответы [ 3 ]

1 голос
/ 20 декабря 2011
1 голос
/ 13 декабря 2011

Вот код, который вы можете использовать для задачи скрипта, которая позволит вам вывести 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: переменная типа Int, используемая для назначения числа строк из потока данных 2.

  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.

0 голосов
/ 19 января 2012

Это то, что я в конце концов придумал! Это самый чистый и простой способ решения этой задачи. Он в основном просто строит строки заголовка и трейлера, а затем добавляет их к набору данных. Кажется, все так просто, как только вы это сделали! Требуются некоторые знания 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();

        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...