user2645263
user2645263

Reputation: 31

get data from SSIS using C#

I using SQL Server 2012 and Visual Studio 2012. I have the follwing code in C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.SqlServer.Dts.Runtime;


namespace ssis_project
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            /*
          SqlConnection con = new SqlConnection(@"Data Source=NICK-PC;Initial Catalog=ssis_project;Integrated Security=True;");
          SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) from users where username='" + userBox.Text + "' and password ='" + passBox.Text + "'", con);
            */

            Microsoft.SqlServer.Dts.Runtime.Application myAplication = new Microsoft.SqlServer.Dts.Runtime.Application();
            Package myPackage = myAplication.LoadPackage(@"D:\SSIS\ssis_project\ssis_project\users.dtsx", null);


            myPackage.Variables["User::uservar"].Value = this.userBox.Text;
            myPackage.Variables["User::passvar"].Value = this.passBox.Text;

            Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = myPackage.Execute();
            if (results == Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success)

                 MessageBox.Show("You are logged as:  " + myPackage.Variables["User::uservar"].Value + " with pass:  " + myPackage.Variables["User::passvar"].Value);

            //DataTable dt = new DataTable();
           //sda.Fill(dt);
           /*
            if (dt.Rows[0][0].ToString() == "1")


                MessageBox.Show("You are logged as:  " + userBox.Text + " with pass:  " + passBox.Text );

            else 
            {
            MessageBox.Show("Please Check your Username and Password");
            }*/
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

If I use connection with SQL Server, is working and show me

MessageBox.Show("You are logged as: " + userBox.Text + " with pass: " + passBox.Text );

or

MessageBox.Show("Please Check your Username and Password");

If I load an SSIS package it doesn't show me that message.

After Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = myPackage.Execute(); I think I will need an if but I don't know how do that. Please help me.

Upvotes: 0

Views: 1093

Answers (1)

Edmund Schweppe
Edmund Schweppe

Reputation: 5132

Your if statement isn't guarding the condition where Package.Execute() returns anything other that DTSExecResult.Success. Since the Execute method can return any one of four DTSExecResult values, you probably want a switch statement:

        DTSExecResult results = myPackage.Execute();
        switch (results)
        {
            case DTSExecResult.Success:
                // do something if the package works
                break;
            case DTSExecResult.Failure:
                // do something else if the package failed
                break;
            case DTSExecResult.Canceled:
                // do yet another something if the package was cancelled
                break;
            case DTSExecResult.Completion:
                // do something completely different if the package ran to completion
                break;
        }

Once you know what the actual return value is, you can troubleshoot further.

Upvotes: 1

Related Questions