开发者

Run SSIS Package through winforms

开发者 https://www.devze.com 2023-03-14 19:39 出处:网络
I need to run a SSIS package through winforms. using System; using System.Collections.Generic; using System.ComponentModel;

I need to run a SSIS package through winforms.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Deployment;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Net.Mime;
public string sPackage = String.Empty;
        public string sConfig = String.Empty;
        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Open Package";
            fDialog.Filter = "SSIS Package (*.dts, *.dtsx)|*.dts;*.dtsx";
            fDialog.InitialDirectory = @"C:\";
            sPackage = fDialog.FileName.ToString();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Open Package";
            fDialog.Filter = "SSIS Package (*.dts, *.dtsx)|*.dts;*.dtsx";
            fDialog.InitialDirectory = @"C:\";
            sConfig = fDialog.FileName.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Microsoft.SqlServer.Dts.Runtime.Wrapper.Application app = new Microsoft.SqlServer.Dts.Runtim开发者_如何学运维e.Wrapper.Application();
            Package package = app.LoadPackage(sPackage,false,null);
            package.ImportConfigurationFile(sConfig);
            DTSExecResult result = package.Execute();
            MessageBox.Show(result.ToString()); 
        }

But this is giving me error at LoadPackage(sPackage,false,null)

Cannot implicitly convert type 'Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSPackage90' to 'Microsoft.SqlServer.Dts.Runtime.Wrapper.Package'. An explicit conversion exists (are you missing a cast?)


I figured out that button3_click should be handled like this

 private void button3_Click(object sender, EventArgs e)
        {
            MyEventListener eventListener = new MyEventListener();             
            Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
            Microsoft.SqlServer.Dts.Runtime.Package pkg = app.LoadPackage(sPackage, eventListener,false);
            Microsoft.SqlServer.Dts.Runtime.DTSExecResult pkgResults = pkg.Execute(null, null, eventListener, null, null);
            MessageBox.Show(pkgResults.ToString()); 
        }

        class MyEventListener : DefaultEvents
        {
            public override bool OnError(DtsObject source, int errorCode, string subComponent,
              string description, string helpFile, int helpContext, string idofInterfaceWithError)
            {
                // Add application-specific diagnostics here.
                MessageBox.Show("Error in " + "/t" + source + "/t" + subComponent + "/t" + description);
                return false;
            }
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号