使用 C# 从 SSIS 获取数据

本文关键字:获取 数据 SSIS 使用 | 更新日期: 2023-09-27 18:34:48

I 使用 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但我不知道该怎么做。请帮助我。

使用 C# 从 SSIS 获取数据

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

知道实际返回值是什么后,可以进一步进行故障排除。