如何在C#中运行SSIS包?

时间:2013-11-11 12:17:14

标签: c# sql sql-server ssis

在.dtsx包中,我有一个“数据流任务”,它将Excel文件中的数据从数据库加载到表中。

然后我有一个“执行SQL任务”,其中包含以下“SQL语句”:

IF OBJECT_ID('tempdb..#code') IS NULL
create table #code
    (
        id int identity,
        code int
    )

IF OBJECT_ID('tempdb..#names') IS NULL
create table #names
    (
        id int identity,
        name nvarchar(50)
    )

IF OBJECT_ID('tempdb..#address1') IS NULL
create table #address1
    (
        id int identity,
        address1 nvarchar(50)
    )

IF OBJECT_ID('tempdb..#address2') IS NULL
create table #address2
    (
        id int identity,
        address2 nvarchar(50)
    )
IF OBJECT_ID('tempdb..#phone') IS NULL
create table #phone
    (
        id int identity,
        phone nvarchar(50)
    )
IF OBJECT_ID('angajati.dbo.AddressFinal', 'U') IS not NULL
drop table AddressFinal
create table AddressFinal(id int identity, US_Address nvarchar(max))

insert into #code(code) select addressname from Address where (id+4)%5=0
insert into #names(name) select addressname from Address where (id+3)%5=0
insert into #address1(address1) select addressname from Address where (id+2)%5=0
insert into #address2(address2) select addressname from Address where (id+1)%5=0
insert into #phone(phone) select addressname from Address where (id+0)%5=0

insert into AddressFinal(US_Address) 
    select convert(nvarchar, code)+ ' ' + name+ ' ' + address1 + ' ' +address2+ ' ' +phone from Address a 
                    inner join #code c on a.id=c.id  
                    inner join #names n on a.id = n.id
                    inner join #address1 ad1 on a.id = ad1.id
                    inner join #address2 ad2 on a.id = ad2.id
                    inner join #phone p on a.id = p.id

select * from AddressFinal      

DROP TABLE #code
DROP TABLE #names
DROP TABLE #address1
DROP TABLE #address2
DROP TABLE #phone
truncate table Address
--drop table AddressFinal`

如果我运行这个SSIS包它正在运行。 在C#中,我有一个包含以下代码的按钮:

    //useLegacyV2RuntimeActivationPolicy="true"  --in app.config
    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 Microsoft.SqlServer.Dts.Runtime;

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

            private void btnStart_Click(object sender, EventArgs e)
            {
                Microsoft.SqlServer.Dts.Runtime.Application myAplication = new Microsoft.SqlServer.Dts.Runtime.Application();
                lblStatus.Text = "Loading package from file system.";
                Package myPackage = myAplication.LoadPackage(@"D:\SSIS\import_excel\import_excel\transfer_address.dtsx", null);   
               // myPackage.Variables["User::myVar"].Value = "test123";
                lblStatus.Text = "Execution package";
                DTSExecResult myResult = myPackage.Execute();
                lblStatus.Text = "Package result: " + myResult.ToString();

            }

            private void textBox1_TextChanged(object sender, EventArgs e)
            {

            }

            private void button1_display_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Hello Earth!");
            }

        }
    }`

如果我运行C#代码,请将excel中的数据加载到表中,但“执行SQL任务”失败。

可以告诉我为什么第二个任务失败了吗?

0 个答案:

没有答案