Monday, July 13, 2009

Load SSIS package from C# code

Load SSIS package from C# code and set run time SSIS package user variable of type object.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SqlServer.Dts.Runtime;
using System.Data.SqlClient;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


if (!IsPostBack)
{
Label1.Visible = false;
SqlDataReader dr;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=(local);initial catalog=DBIS;user id=sa;password=ciitdc#123;";
conn.Open();
SqlCommand com = new SqlCommand();
com.CommandText = "select * from files";
com.CommandType = CommandType.Text;
com.Connection = conn;

dr = com.ExecuteReader();

while (dr.Read())
{

checkedListBox1.Items.Add(dr[1].ToString());
}

conn.Close();
}

}
protected void Button1_Click(object sender, EventArgs e)
{
ArrayList arrayList = new ArrayList();

for (int j = 0; j < checkedListBox1.Items.Count; j++)
{
if (checkedListBox1.Items[j].Selected )
{
arrayList.Add(checkedListBox1.Items[j].ToString());

}

}

String[] filesnames = (String[])arrayList.ToArray(typeof(string));

Application app = new Application();
Package package = null;

//package = app.LoadFromSqlServer("\\Package", "servername", "sa", "password", null);
package = app.LoadPackage(@"E:\Demo1\DBISTest\DBISTest\Package.dtsx", null);
package.Variables["files"].Value = filesnames;
DTSExecResult ssisPackageResults = package.Execute();
Label1.Visible = true;
Label1.Text = ssisPackageResults.ToString();

}
}