使用C#从SSIS获取数据

时间:2013-10-11 08:19:47

标签: c# sql-server ssis

我使用的是SQL Server 2012和Visual Studio 2012。 我在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)
        {

        }
    }
}

如果我使用与SQL Server的连接,正在工作并告诉我

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

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

如果我加载了一个SSIS包,它就不会向我显示该消息。

Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = myPackage.Execute();之后我想我需要一个if,但我不知道怎么做。请帮帮我。

1 个答案:

答案 0 :(得分:1)

if语句无法保护Package.Execute()返回DTSExecResult.Success以外的任何内容的情况。由于Execute方法可以返回四个DTSExecResult值中的任意一个,因此您可能需要switch语句:

        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;
        }

一旦您知道实际的返回值是什么,就可以进一步排除故障。