简体   繁体   中英

reading a SSIS package variable from it's C# asp launcher

I have to modify an existing SSIS package, to launch it from a web site. Currently, this package is launched on demand by double clicking it, shows a form to ask for an excel file location and some database credentials that are stored in package variables, and then loads data from the excel file into a DB. Since there are many errors that can occur in the process, there is a package variable that holds an internal state, to inform the user which part of the process failed.

Given that I have to launch the package from a web site, as a first approach I have split the package in two, a master package that gets the information from user, executes the slave package by passing the user parameters through package variables, gets the child package internal state and then it finishes by informing the user the final state of this process. The communication between packages is being done by using variables with the same name and package configuration (main package variables). This is true for all variables except for the internal state one, that exists just in the parent, but is used in the child. Since both share the same context, it works ok.

Now that the child package is isolated, I'm trying to replace the master one with a C# asp site. Currently I'm able to get the user parameters through a webform and execute the package, but I can't figure how to read the child's internal state variable from the web app.

This internal value is an integer from 0 to 12, where 0 means ok and any other means that something went wrong with loading a table, executing a SP or something else.

There is a way to get this package variable value from the web app, when the package finishes? Otherwise, I just realized that this could be wrote in a log file that could be read by the web app, but I was wondering if there is a more wise solution.


Just to let you know, this is how I'm passing variables from the web app to the package. The package is configured to set its variables from primary/main package variables.

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SqlServer.Dts.Runtime;


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Application app = new Application();
        Package package = null;
        String PackagePath = "";
        try
        {  

            string fileName = FileUpload1.PostedFile.FileName.ToString();
            fileName = Server.MapPath("App_Data//" + System.IO.Path.GetFileName(fileName));
            FileUpload1.PostedFile.SaveAs(fileName);            

            //Load DTSX    
            PackagePath = @"C:\Program Files\Microsoft SQL Server\100\DTS\Packages\Null Project\Package.dtsx";
            package = app.LoadPackage(PackagePath, null);            

            //Global package variables (same name)
            Hashtable param = new Hashtable();
            param["ServidorOrigen"] = "SQL_SERVER";
            param["UserOrigen"] = "user";
            param["PassOrigen"] = "pass";
            param["BaseDatosOrigen"] = "test_database";
            param["EstadoConexion"] = 0;
            param["EstadoPaquete"] = 0;
            param["ExcelRuta"] = fileName.ToString();

            Variables vars = null;

            foreach (DictionaryEntry entry in param)
            {
                package.VariableDispenser.LockOneForWrite(entry.Key.ToString(), ref vars);

                try
                {
                    vars[entry.Key.ToString()].Value = entry.Value.ToString();
                }
                catch
                {
                    throw new Exception("variable " + entry.Key.ToString() + " not found in package");
                }
                finally
                {
                    vars.Unlock();
                }
            }

            //Execute DTSX
            Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = package.Execute();

            //Collects debugging info
            using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/App_Data/log.txt"), true))
            {
                if (!package.Errors.Count.Equals(0)){
                    _testData.WriteLine(package.Errors.Count.ToString()); // Write the file.

                    ErrorEnumerator myEnumerator = package.Errors.GetEnumerator();
                    int i = 0;
                    while ((myEnumerator.MoveNext()) && (myEnumerator.Current != null))
                        _testData.WriteLine("[{0}] {1}", i++, myEnumerator.Current.Description);
                }
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
}

You can store the state in a database table the same way you would with your log file and then just have your web app read that at a given interval.

I'm not sure how you are passing your variables from the web app to the ssis, but you could look into the ssis configuration stuff storing in sql databases.

I have a similar thing I do.

  1. Config stuff saved to database from web app.
  2. Web app calls a sql job.
  3. Job starts ssis package.
  4. Web app queries every minute to see if the job has finished and returns succeeded or failed to user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM