Click here to Skip to main content
Click here to Skip to main content

Excecute SSIS package (DTSX) from ASP.Net

By , 16 Sep 2008
 

Introduction

I got a task to develop the web page that execute SQL server 2005 SSIS package. With less knowledge of SSIS it was difficult for me to get the solution right at first time. But with online help I was able to crack the problem. I created a simple DTSX package which takes spreadsheet data and pumps it into a database table and then I execute store procedure inside the package to perform manipulation in database table. These whole bunch of process are then executed through asp.net. Below I have demonstrate the steps to achieve this.

Creation of SSIS Package

Prerequisite:

1)Database table:

CREATE TABLE [dbo].[tblBusiness](
	[Business_ID] [numeric](18, 0) NULL,
	[Business_Name] [nvarchar](50) NULL
) ON [PRIMARY]

2)Excel data:BusinessTemplate.xls

SSIS Package Workflow design:

  • Invoke SQL task
  • to delete the tblBusiness data.
  • Load Excel data into database table tblBusiness.
  • Invoke SQL task to update database table tblBusiness.
  • Step 1: Load Excel data into database table tblBusiness.

    1. Click on ‘SSIS Import Export Wizard’ Under ‘Project’ Menu.

    2.Click on Next Button.

    3.Choose Source and click next.

    In our case source is Excel .The path specified will be overwritten when assigned through .net code.

    E.g. package.Connections["SourceConnectionExcel"].ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=" + tbFileName.Text + ";Extended Properties=Excel 8.0; ";

    3.Choose Destination and click next.

    4.Choose Copy data from one or more table and Click next

    5.Complete Wizard Screen appears and Click finish.

    Once the above steps are completed one can see the following views

    Under control Flow Tab:

    Under Data Flow Tab:

    Note: In Connection Manager two connection objects appears:

  • Excel Connection Object:SourceConnectionExcel
  • SQL Server: DestinationConnectionOLEDB
  • Double Click on each object to verify the connection string and save it again.

    Just test whether Dataflow is correctly built.If turns green then dataflow is successfully executed . If turns RED then there is problem with connection string.

    Step 2: Create Sql Task Script to delete the database table.

    1. Create Global Package level variable.These variables are input parameters to package. So the package having sql storeprocedure can consume these input param.

    2)Double Click on ‘Preparation SQL Task’ under Control Flow Tab.

    We have taken "EXEC DeleteBusinessEntry ?" this procedure is going to take input parameter @Business_ID .

    Create Storeprocedure:

    CREATE PROCEDURE DeleteBusinessEntry 
    	@Business_ID bigint	
    AS
    BEGIN
    	Delete from tblBusiness where Business_ID=@Business_ID
    END
    

    3)Click on ParameterMapping so as to map Global user defined input varible to storeproc input variable. It is case sensitive.Also take care of the datatype and size of the datatype.Each variable should have different ParameterName in it.

    Step 2: Create Sql Task Script to update database table content that is loaded with excel data.

    Drag and Drop ‘Execute Sql Task’ from toolbox on to control flow screen. Connect Prepare Sql task to dataflow task.Also connect Dataflow task to SQl Script task object.

    2)Double Click on ‘Execute Sql Task’ under Control Flow Tab.

    We have taken "EXEC UpdateBusinessEntry ?,?" this procedure is going to take two input parameters @Business_ID /@Business_Name.

    Create Storeprocedure:

    CREATE PROCEDURE UpdateBusinessEntry 
    	@Business_ID bigint	,
    	@Business_Name varchar(100)	
    AS
    BEGIN
    	Update  tblBusiness 
    	Set Business_Name=@Business_Name
    	where Business_ID=@Business_ID
    END
    
    

    3)Click on ParameterMapping so as to map Global user defined input varible to storeproc input variable. It is case sensitive.Also take care of the datatype and size of the datatype. Each variable should have different ParameterName in it.

    On completion of above three steps run the package .

    Execute SSIS Package Using ASP.Net

    The Important reference : Microsoft.SqlServer.ManagedDTS

    The below code snippet is self explanatory

           using Microsoft.SqlServer.Dts.Runtime;
            
           protected void btnExecute_Click(object sender, EventArgs e)
           {
                Application app = new Application();
                Package package = null;
                try
                {
                string fileName = 
    Server.MapPath(System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName.ToString()));
                FileUpload1.PostedFile.SaveAs(fileName);
                
                //Load DTSX
                package = 
    app.LoadPackage(@"D:\SSIS_ASP_NET\SSIS_ASP_NET_DEMO\SSIS_ASP_NET_DEMO\Package1.dtsx", null);
                
                //Global Package Variable
                Variables vars = package.Variables;
                vars["Business_ID"].Value = txtBusinessID.Text;
                vars["Business_Name"].Value = txtBusinessName.Text;
                
                //Specify Excel Connection From DTSX Connection Manager
                package.Connections["SourceConnectionExcel"].ConnectionString =
     "provider=Microsoft.Jet.OLEDB.4.0;data source=" + fileName + ";Extended Properties=Excel 8.0; ";
                
                //Execute DTSX.
                Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = package.Execute();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                package.Dispose();
                package = null;
            }
        }
    
            

    The important factor in above code is to assign the data source of excel file to the SSIS package as package.Connections["SourceConnectionExcel"].ConnectionString

    Html Design

    <html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; >
    <head runat="&quot;server&quot;">
        <title>Execute SSIS</title>
    </head>
    <body>
        <form id=&quot;form1&quot; runat="&quot;server&quot;">
        <div>
             <div style=&quot;text-align: left&quot;>
                <table>
                       <tr>
                        <td style=&quot;height: 26px&quot; colspan=&quot;2&quot;>
                       <h2>Execute SSIS Package</h2>     </td>
                    </tr>
                    <tr>
                        <td style=&quot;width: 100px; height: 26px&quot;>
                            <strong>
        
            Business ID</strong></td>
                        <td style=&quot;width: 100px; height: 26px&quot;>
                            <asp:TextBox ID=&quot;txtBusinessID&quot; runat="&quot;server&quot;"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td style=&quot;width: 100px&quot;>
                            <strong>BusinessName</strong></td>
                        <td style=&quot;width: 100px&quot;>
                            <asp:TextBox ID=&quot;txtBusinessName&quot; runat="&quot;server&quot;"></asp:TextBox></td>
                    </tr>
                       <tr>
                        <td style=&quot;width: 100px&quot;>
                            <strong>Upload Excel</strong></td>
                        <td style=&quot;width: 100px&quot;>
        
            <asp:FileUpload ID=&quot;FileUpload1&quot; runat="&quot;server&quot;" /></td>
                    </tr>   <tr>
                        <td style=&quot;width: 100px&quot;>
            </td>
                        <td style=&quot;width: 100px&quot;>
            <asp:Button ID=&quot;btnExecute&quot; runat="&quot;server&quot;" Text=&quot;Execute SSIS&quot; OnClick=&quot;btnExecute_Click&quot; /></td>
                    </tr>
                </table>
            </div>
        
            <br />
              <br />
             <br />
            <br />
            </div>
        </form>
    </body>
    </html>
    

    Reference

    Before implementing this into development .Please refer this article.This article describes about the prerequisite for production deployment.http://www.codeproject.com/KB/aspnet/Deployment_SSIS_NET.aspx

    Conclusion

    Hope this article will serve the purpose.Any suggestions or corrections are most welcome.

    License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    About the Author

    santosh poojari
    Technical Lead
    India India
    Member
    He is presently working as tech arch in one of the leading IT company.He has total 10 years of experience in C#.net. He is a B.E graduate in Computers from Bombay University.
     
    Most of his experiences are in designing architect for end to end solutions. His interest areas are WCF,Spring.net,Architecture- Model View Presenter,UML,Webservice,Performance Engineering/tuning,Design patterns,Generics,Enterprise Library,Regular expressions,Silverlight and WWF.
    www.santoshpoojari.blogspot.com

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    AnswerRe: Export size problemmembersantosh poojari28 Dec '09 - 21:16 
    Try this out in web.config ,hope this help
    <httpRuntime executionTimeout="240" maxRequestLength="10480"/>
     
    Happy Coding
    "San"
     

     

    GeneralGood ArticlememberVuyiswa Maseko25 Oct '09 - 21:40 
    I must say this is a nice Article. Keep up the Good Work. i give you 5.
     
    Vuyiswa Maseko,
     
    Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
     
    C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
    http://www.vuyiswamaseko.com
    vuyiswa@its.co.za
    http://www.itsabacus.co.za/itsabacus/

    GeneralRe: Good Articlemembersantosh poojari28 Oct '09 - 17:07 
    Thanks A Lot!
     
    Happy Coding
    "San"
     

     

    GeneralRe: Good ArticlememberVuyiswa Maseko28 Oct '09 - 20:13 
    One thing i forgot to ask you.
     
    you used this
     
                //Specify Excel Connection From DTSX Connection Manager
                package.Connections[&quot;SourceConnectionExcel&quot;].ConnectionString =
     &quot;provider=Microsoft.Jet.OLEDB.4.0;data source=&quot; + fileName + &quot;;Extended Properties=Excel 8.0; &quot;;
    
     
    and i want to use SQL , can you please convert it to SQL for me.
     
    Thanks
     
    Vuyiswa Maseko,
     
    Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
     
    C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
    http://www.vuyiswamaseko.com
    vuyiswa@its.co.za
    http://www.itsabacus.co.za/itsabacus/

    GeneralRe: Good Articlemembersantosh poojari6 Nov '09 - 1:04 
    package.Connections["SourceConnectionSQL"].ConnectionString = ConfigurationManager.Appsetting["Connectionstring"];
    You can store your connectionstring appsetting in web config and then retieve here. Also the SourceConnectionSQL is your object connection in dtsx.Hope this help you.
     
    Happy Coding
    "San"
     

     

    GeneralRe: Good ArticlememberVuyiswa Maseko6 Nov '09 - 1:28 
    This will help me.
     
    Thanks Good Work
     
    Vuyiswa Maseko,
     
    Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
     
    C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
    http://www.vuyiswamaseko.com
    vuyiswa@its.co.za
    http://www.itsabacus.co.za/itsabacus/

    Generalfaster extraction and loading on SSIS.memberblackpower2k725 Apr '09 - 19:54 
    hi...
    check out the below link for getting some tips over faster extraction and loading on SSIS.
     
    http://www.sqllion.com/
     
    You will know me better sooner...

    QuestionVariable not Found errormemberPastdairyman17 Mar '09 - 9:26 
    In my web app a button is used to start the ssis package.
     
    Dim pkgLocation As String
    Dim pkg As New Package
    Dim app As New Application
    Dim vars As Variables
    Dim pkgResults As DTSExecResult
     
    pkgLocation = "F:\....\Package.dtsx"
     
    'Global Package Variable
    vars = pkg.Variables
    vars("GetMonth").Value = Me.ddlGetMonth.SelectedValue
     
    pkg = app.LoadPackage(pkgLocation, Nothing)
     
    pkgResults = pkg.Execute()
     
    This code worked until I added the two lines for the variable, now I get a error "Variable not found ..." I added the reference ManageDTS.
    In the ssis package under variables I have the variable listed Name: GetMonth, Scope: Package, DataType: int32, Value: 1
     
    Thank you for any help
    GeneralNice Amigo..memberDomingo M. Asuncion15 Sep '08 - 21:55 
    I learn it..
     
    Thanks,
    Domingo Asuncion
    QuestionExecuting SSIS package from a streammemberm_krimson18 Aug '08 - 23:27 
    First of all, nice article. Very easy to follow.
     
    I think it would be interesting to include the dtsx file embedded in a library so its deployment is easier. In this case, we would like to run the package from that stream. Of course, we can create a temporary file but that means that write permissions have to be granted, and I'm trying to avoid that.
     
    Is there any way of doing this?
    AnswerRe: Executing SSIS package from a streammembersantosh poojari1 Sep '08 - 3:54 
    Yes We can do that,We can have dtsx in web directory as any repository.In that case we do not require to give any access rights.Are you trying to intend this?
     
    Happy Coding
    "San"
     

     

    GeneralExcellent reference guide.memberGautam Sharma28 Jul '08 - 0:22 
    Excellent reference guide. good artical santosh.
     
    Happy coding

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.6.130516.1 | Last Updated 17 Sep 2008
    Article Copyright 2008 by santosh poojari
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid