June 05, 2013

Dynamic Fixed Width Connection Part II Adding FileSource



/*
   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.Data;
using Microsoft.SqlServer.Dts.Runtime;
using RunTimeWrapper = Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Data.SqlClient;
using Microsoft.SqlServer.Dts.Pipeline;
using PipeLineWrapper = Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using System.Text;


namespace ST_394d7e0c32ff40af81e953483ef3c2cf.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.
*/
        Package _pgk = new Package();
        Application _app = new Application();
        public void Main()
        {
            Variables vars = null;
            Dts.VariableDispenser.LockForRead("User::strTabListCmd");
            Dts.VariableDispenser.LockForRead("User::strSqlCmdFilter");
            Dts.VariableDispenser.LockForRead("User::strColumnCmd");
            Dts.VariableDispenser.LockForRead("User::strSavePath");
            Dts.VariableDispenser.LockForRead("User::strFileDirectory");
            Dts.VariableDispenser.GetVariables(ref vars);

            try
            {

                String CmdTabList = vars["User::strTabListCmd"].Value.ToString();
                String CmdFilter = vars["User::strSqlCmdFilter"].Value.ToString();
                String CmdColumns = vars["User::strColumnCmd"].Value.ToString();
                String FileDirectory = vars["User::strFileDirectory"].Value.ToString();

                String ServerName = @""; // Remove my own reference 
                String DatabaseName = @"";
                String ConnectionString = "Server=" + ServerName + ";Database=" + DatabaseName + ";Trusted_Connection=True;";
                
                DataTable _DataTable = ExecuteCommand(ConnectionString, CmdTabList);
                int CntRows = _DataTable.Rows.Count;
                foreach (DataRow row in _DataTable.Rows)
                {
                    String FileName = (String)row["FileName"];
                    String TabName = (String)row["TabName"];
                    String Filter = CmdFilter;
                    String Fiter2 = Filter.Replace("<<Filter>>", TabName);
                    StringBuilder NewCmd = new StringBuilder();
                    NewCmd.Append(CmdColumns);
                    NewCmd.Append(Fiter2);
                   
                    DataTable connColumns = ExecuteCommand(ConnectionString, NewCmd.ToString());
                    ConnectionManager _ConnMgr = AddFixedWith(connColumns, TabName, FileDirectory + "\\" + FileName + ".DAT");
                    PipeLineWrapper.MainPipe dataflow = AddWorkFlowComponent(TabName);
                    PipeLineWrapper.IDTSComponentMetaData100 FileSource = AddFileSourceComponent(dataflow, _ConnMgr, TabName);

                    connColumns = null;
                    NewCmd = null;
                    Filter = null;
                    TabName = null;
                    Fiter2 = null;
                  
                }
              
                _app.SaveToXml("C:\\temp\\test02.dtsx", _pgk, null);
                //Dispose of table 
                // TODO: Add your code here
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            catch 
            {
                
                Dts.TaskResult = (int)ScriptResults.Failure;

            }
        }
        private DataTable ExecuteCommand(String ConnectionString, String qrty)
        {
            SqlConnection BDEConnection = new SqlConnection(ConnectionString);
            DataTable rtData = new DataTable();
            BDEConnection.Open();
            SqlCommand  SqlCmd = new SqlCommand(qrty,BDEConnection);
            SqlDataReader DtReader = SqlCmd.ExecuteReader();
            rtData.Load(DtReader);
            BDEConnection.Close();
            return rtData;
        }
        private ConnectionManager AddFixedWith(DataTable dtColumns, String ConnectionName, String ConnectionString)
        {
            ConnectionManager flatConn = _pgk.Connections.Add("FLATFILE");
            flatConn.Name = ConnectionName;
            flatConn.ConnectionString = ConnectionString;
            flatConn.Properties["Format"].SetValue(flatConn, "FixedWidth");
            flatConn.Properties["CodePage"].SetValue(flatConn, "37");
            flatConn.Properties["DataRowsToSkip"].SetValue(flatConn, 1);
            flatConn.Properties["RowDelimiter"].SetValue(flatConn, "\r\n");
            flatConn.Properties["LocaleID"].SetValue(flatConn, "1033");
            //Inner object
            RunTimeWrapper.IDTSConnectionManagerFlatFile100 ConnectionFlatFile = (RunTimeWrapper.IDTSConnectionManagerFlatFile100)flatConn.InnerObject;


            foreach (DataRow row in dtColumns.Rows)
            {
                RunTimeWrapper.IDTSConnectionManagerFlatFileColumn100 FlatFileColumn100 = ConnectionFlatFile.Columns.Add();
                FlatFileColumn100.ColumnType = "FixedWidth";
                FlatFileColumn100.ColumnWidth = (int)row["FieldLength"];
                FlatFileColumn100.MaximumWidth = (int)row["FieldLength"];
                int Scale = (int)row["Scale"];
                switch (Scale)
                {
                    case -1:
                    FlatFileColumn100.DataType = RunTimeWrapper.DataType.DT_WSTR;
                    break;
                    default:
                      FlatFileColumn100.DataType = RunTimeWrapper.DataType.DT_BYTES;
                    break;
                    


                }
                          
                RunTimeWrapper.IDTSName100 ColName = (RunTimeWrapper.IDTSName100)FlatFileColumn100;
                ColName.Name = (String) row["FieldLevelName"];
               
            }
            return flatConn;
        }
        private PipeLineWrapper.MainPipe AddWorkFlowComponent(String ComponentName)
        {
            _pgk.Executables.Add("STOCK:PipelineTask");
            TaskHost _TaskHost = (TaskHost)_pgk.Executables[0];
            PipeLineWrapper.MainPipe dataFlowTask = (PipeLineWrapper.MainPipe)_TaskHost.InnerObject;
            _TaskHost.Name = ComponentName;
            _TaskHost.Properties["DefaultBufferMaxRows"].SetValue(_TaskHost, "1000000");

            return dataFlowTask;

        }
        private PipeLineWrapper.IDTSComponentMetaData100 AddFileSourceComponent(PipeLineWrapper.MainPipe dataFlowTask, ConnectionManager flaFileCon, String ComponentName)
        {
            PipeLineWrapper.IDTSComponentMetaData100 FileSource = dataFlowTask.ComponentMetaDataCollection.New();
            FileSource.Name = "SourceFile" + ComponentName;
            FileSource.ComponentClassID = "DTSAdapter.FlatFileSource.2";
            PipeLineWrapper.CManagedComponentWrapper instanceFileSource = FileSource.Instantiate();
            instanceFileSource.ProvideComponentProperties();
            //Set source connection
            FileSource.RuntimeConnectionCollection[0].ConnectionManagerID = flaFileCon.ID;
            FileSource.RuntimeConnectionCollection[0].ConnectionManager = DtsConvert.GetExtendedInterface(flaFileCon);

            //Reinitialize Flat File source metadata, 
            instanceFileSource.AcquireConnections(null);
            instanceFileSource.ReinitializeMetaData();
            instanceFileSource.ReleaseConnections();
            return FileSource;
        }


    }
}

June 04, 2013

Dynamic Generation of FlatFile FixedWidth Part I


/*
   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.Data;
using Microsoft.SqlServer.Dts.Runtime;
using RunTimeWrapper = Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Data.SqlClient;
using Microsoft.SqlServer.Dts.Pipeline;
using PipeLineWrapper = Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using System.Text;


namespace ST_394d7e0c32ff40af81e953483ef3c2cf.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.
*/
        Package _pgk = new Package();
        Application _app = new Application();
        public void Main()
        {
            Variables vars = null;
            Dts.VariableDispenser.LockForRead("User::strTabListCmd");
            Dts.VariableDispenser.LockForRead("User::strSqlCmdFilter");
            Dts.VariableDispenser.LockForRead("User::strColumnCmd");
            Dts.VariableDispenser.LockForRead("User::strSavePath");
            Dts.VariableDispenser.LockForRead("User::strFileDirectory");
            Dts.VariableDispenser.GetVariables(ref vars);

            try
            {

                String CmdTabList = vars["User::strTabListCmd"].Value.ToString();
                String CmdFilter = vars["User::strSqlCmdFilter"].Value.ToString();
                String CmdColumns = vars["User::strColumnCmd"].Value.ToString();
                String FileDirectory = vars["User::strFileDirectory"].Value.ToString();

                String ServerName = @"";
                String DatabaseName = @"";
                String ConnectionString = "Server=" + ServerName + ";Database=" + DatabaseName + ";Trusted_Connection=True;";
                
                DataTable _DataTable = ExecuteCommand(ConnectionString, CmdTabList);
                int CntRows = _DataTable.Rows.Count;
                foreach (DataRow row in _DataTable.Rows)
                {
                    String FileName = (String)row["FileName"];
                    String TabName = (String)row["TabName"];
                    String Filter = CmdFilter;
                    String Fiter2 = Filter.Replace("<<Filter>>", TabName);
                    StringBuilder NewCmd = new StringBuilder();
                    NewCmd.Append(CmdColumns);
                    NewCmd.Append(Fiter2);
                   
                    DataTable connColumns = ExecuteCommand(ConnectionString, NewCmd.ToString());
                    AddFixedWith(connColumns, TabName, FileDirectory + "\\" + FileName + ".DAT");
                    connColumns = null;
                    NewCmd = null;
                    Filter = null;
                    TabName = null;
                    Fiter2 = null;
                }
              
                _app.SaveToXml("C:\\temp\\test02.dtsx", _pgk, null);
                //Dispose of table 
                // TODO: Add your code here
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            catch 
            {
                
                Dts.TaskResult = (int)ScriptResults.Failure;

            }
        }
        private DataTable ExecuteCommand(String ConnectionString, String qrty)
        {
            SqlConnection BDEConnection = new SqlConnection(ConnectionString);
            DataTable rtData = new DataTable();
            BDEConnection.Open();
            SqlCommand  SqlCmd = new SqlCommand(qrty,BDEConnection);
            SqlDataReader DtReader = SqlCmd.ExecuteReader();
            rtData.Load(DtReader);
            BDEConnection.Close();
            return rtData;
        }
        private void AddFixedWith(DataTable dtColumns,String ConnectionName,String ConnectionString)
        {
            ConnectionManager flatConn = _pgk.Connections.Add("FLATFILE");
            flatConn.Name = ConnectionName;
            flatConn.ConnectionString = ConnectionString;
            flatConn.Properties["Format"].SetValue(flatConn, "FixedWidth");
            flatConn.Properties["CodePage"].SetValue(flatConn, "37");
            flatConn.Properties["DataRowsToSkip"].SetValue(flatConn, 1);
            flatConn.Properties["RowDelimiter"].SetValue(flatConn, "\r\n");
            flatConn.Properties["LocaleID"].SetValue(flatConn, "1033");
            //Inner object
            RunTimeWrapper.IDTSConnectionManagerFlatFile100 ConnectionFlatFile = (RunTimeWrapper.IDTSConnectionManagerFlatFile100)flatConn.InnerObject;


            foreach (DataRow row in dtColumns.Rows)
            {
                RunTimeWrapper.IDTSConnectionManagerFlatFileColumn100 FlatFileColumn100 = ConnectionFlatFile.Columns.Add();
                FlatFileColumn100.ColumnType = "FixedWidth";
                FlatFileColumn100.ColumnWidth = (int)row["FieldLength"];
                FlatFileColumn100.MaximumWidth = (int)row["FieldLength"];
                int Scale = (int)row["Scale"];
                switch (Scale)
                {
                    case -1:
                    FlatFileColumn100.DataType = RunTimeWrapper.DataType.DT_STR;
                    break;
                    default:
                      FlatFileColumn100.DataType = RunTimeWrapper.DataType.DT_BYTES;
                    break;
                    


                }
                
                RunTimeWrapper.IDTSName100 ColName = (RunTimeWrapper.IDTSName100)FlatFileColumn100;
                ColName.Name = (String) row["FieldLevelName"];
               
            }

            

        }


    }
}

April 22, 2013

Convert Excel to Standard File Enumeration SAVEAS


#region Help:  Introduction to the script task
/* The Script Task allows you to perform virtually any operation that can be accomplished in
 * a .Net application within the context of an Integration Services control flow. 
 * 
 * Expand the other regions which have "Help" prefixes for examples of specific ways to use
 * Integration Services features within this script task. */
#endregion


#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
#endregion

namespace ST_c2b138062fc148478472bed5f4561ecf
{
    /// <summary>
    /// ScriptMain is the entry point class of the script.  Do not change the name, attributes,
    /// or parent of this class.
    /// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
        #region Help:  Using Integration Services variables and parameters in a script
        /* To use a variable in this script, first ensure that the variable has been added to 
         * either the list contained in the ReadOnlyVariables property or the list contained in 
         * the ReadWriteVariables property of this script task, according to whether or not your
         * code needs to write to the variable.  To add the variable, save this script, close this instance of
         * Visual Studio, and update the ReadOnlyVariables and 
         * ReadWriteVariables properties in the Script Transformation Editor window.
         * To use a parameter in this script, follow the same steps. Parameters are always read-only.
         * 
         * Example of reading from a variable:
         *  DateTime startTime = (DateTime) Dts.Variables["System::StartTime"].Value;
         * 
         * Example of writing to a variable:
         *  Dts.Variables["User::myStringVariable"].Value = "new value";
         * 
         * Example of reading from a package parameter:
         *  int batchId = (int) Dts.Variables["$Package::batchId"].Value;
         *  
         * Example of reading from a project parameter:
         *  int batchId = (int) Dts.Variables["$Project::batchId"].Value;
         * 
         * Example of reading from a sensitive project parameter:
         *  int batchId = (int) Dts.Variables["$Project::batchId"].GetSensitiveValue();
         * */

        #endregion

        #region Help:  Firing Integration Services events from a script
        /* This script task can fire events for logging purposes.
         * 
         * Example of firing an error event:
         *  Dts.Events.FireError(18, "Process Values", "Bad value", "", 0);
         * 
         * Example of firing an information event:
         *  Dts.Events.FireInformation(3, "Process Values", "Processing has started", "", 0, ref fireAgain)
         * 
         * Example of firing a warning event:
         *  Dts.Events.FireWarning(14, "Process Values", "No values received for input", "", 0);
         * */
        #endregion

        #region Help:  Using Integration Services connection managers in a script
        /* Some types of connection managers can be used in this script task.  See the topic 
         * "Working with Connection Managers Programatically" for details.
         * 
         * Example of using an ADO.Net connection manager:
         *  object rawConnection = Dts.Connections["Sales DB"].AcquireConnection(Dts.Transaction);
         *  SqlConnection myADONETConnection = (SqlConnection)rawConnection;
         *  //Use the connection in some code here, then release the connection
         *  Dts.Connections["Sales DB"].ReleaseConnection(rawConnection);
         *
         * Example of using a File connection manager
         *  object rawConnection = Dts.Connections["Prices.zip"].AcquireConnection(Dts.Transaction);
         *  string filePath = (string)rawConnection;
         *  //Use the connection in some code here, then release the connection
         *  Dts.Connections["Prices.zip"].ReleaseConnection(rawConnection);
         * */
        #endregion


/// <summary>
        /// This method is called when this script task executes in the control flow.
        /// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        /// To open Help, press F1.
        /// </summary>
public void Main()
{
// TODO: Add your code here
            try
            {
                Excel.Application xlApp = new Excel.Application();
                Excel.Workbook xlWrkBk = xlApp.Workbooks.Open(@"C:\temp\EIM\TestFile.xlsx");
                xlWrkBk.SaveAs(@"C:\temp\EIM\TestFile.csv", Excel.XlFileFormat.xlCSVWindows);
                xlWrkBk.Close(false);
                xlApp.Quit();

                xlWrkBk = null;
                xlApp = null;

                Dts.TaskResult = (int)ScriptResults.Success;
            }
            catch
            {
                Dts.TaskResult = (int)ScriptResults.Failure;
            }

}

        #region ScriptResults declaration
        /// <summary>
        /// This enum provides a convenient shorthand within the scope of this class for setting the
        /// result of the script.
        /// 
        /// This code was generated automatically.
        /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

}
}

Contact Form

Name

Email *

Message *