Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7097837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T10:59:06+00:00 2026-05-28T10:59:06+00:00

I have a SSIS package which exports data from a query into a flat

  • 0

I have a SSIS package which exports data from a query into a flat file, which will be used to import into a data warehouse. One of my requirements is to add a header row with the current date, and a footer row with total row count.

I would like to do this ideally in one script component or task using C# for tidiness in the package. I’m a noob when it comes to writing code. How can this be done? I’ve looked around on the net but can’t seem to find anything close enough to what I want.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T10:59:07+00:00Added an answer on May 28, 2026 at 10:59 am

    This is what I eventually came up with! It is the cleanest, most simple way I can find of doing this task. It basically just builds up the header and trailer rows then appends to the dataset. Seems so simple once you’ve done it! It takes a bit of knowledge of C#, however it is well worth it than trying to do it in 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();
    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple SSIS package which reads data from flat file and insert into
I have an ssis package...which read data from fixed-column-width text file into DB table.
I have an SSIS package which reads an Excel File (Data Flow Source) and
I have an SSIS package that exports data from a table on a SQL
I have a SSIS package which reads an Excel File (Data Flow Source) and
i want to create one ssis package which takes values from flat file and
I have an SSIS package that queries data from a view using an SQL
I have created a (SSIS) package which contains SQL procedures for transfer of data
I have made a SSIS package to create an XML file, which works fine
I have an SSIS package that takes data from Tables in an SQL database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.