使用c#windows形式调用ssis包(包含ssis包中要传递的变量)
Calling a ssis package using c# windows form(containing variable to be passed in the ssis package)
我有一个 SSIS 包。我设计了一个 windows 表单,包含标签、文本框和按钮来调用 ssis。但我被困在一个场景中。
就像假设我在 ssis 中有 5 个变量,其中一个变量名表示帐号需要从 windows 表单文本框获取。
我的文本框代码是
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox objTextBox = (TextBox)sender;
string theText = objTextBox.Text;
string pkgLocation;
Package pkg = null;
Microsoft.SqlServer.Dts.Runtime.Application app;
DTSExecResult pkgResults;
Microsoft.SqlServer.Dts.Runtime.Variables myVars = pkg.Variables;
pkgLocation =
@"C:\Users\Visual Studio 2008\Projects" +
@"\Integration Services Project1\xyz.dtsx";
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadPackage(pkgLocation, null);
myVars["Account_number"].Value = theText;
pkgResults = pkg.Execute(null, myVars,null,null,null);
}
这里的问题是,当在 window 表单中输入一位数字,比如 9 时,它将开始执行 package.Here 我实际上想让用户输入完整的 account_Number 和 运行 单击按钮时的包。
请告诉我代码中的问题是什么,或者我应该添加什么才能在单击按钮时获得 account_number?
您正在 textBox1_TextChanged
事件中编写代码,这就是每次您在文本框中键入字母或数字时代码都会执行的原因。每次你在文本框中输入内容时 TextChanged
事件都会被触发。
相反,如果您希望代码在 button_click
上执行,那么您应该在 button_click
事件中编写代码。
调用包 button_click。这样,每次您在文本框中输入内容时,都不会执行该包。
编辑
您的 pkg
对象为空,这就是您收到该错误的原因。加载包后移动 Microsoft.SqlServer.Dts.Runtime.Variables myVars = pkg.Variables;
部分。
pkg = app.LoadPackage(pkgLocation, null);
Microsoft.SqlServer.Dts.Runtime.Variables myVars = pkg.Variables;
private void button_clicked(object sender, EventArgs e)
{
string theText = textBox1.Text;
string pkgLocation;
Package pkg = null;
Microsoft.SqlServer.Dts.Runtime.Application app;
DTSExecResult pkgResults;
pkgLocation =
@"C:\Users\Visual Studio 2008\Projects" +
@"\Integration Services Project1\xyz.dtsx";
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadPackage(pkgLocation, null);
Microsoft.SqlServer.Dts.Runtime.Variables myVars = pkg.Variables;
myVars["Account_number"].Value = theText;
pkgResults = pkg.Execute(null, myVars,null,null,null);
}
我有一个 SSIS 包。我设计了一个 windows 表单,包含标签、文本框和按钮来调用 ssis。但我被困在一个场景中。 就像假设我在 ssis 中有 5 个变量,其中一个变量名表示帐号需要从 windows 表单文本框获取。
我的文本框代码是
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox objTextBox = (TextBox)sender;
string theText = objTextBox.Text;
string pkgLocation;
Package pkg = null;
Microsoft.SqlServer.Dts.Runtime.Application app;
DTSExecResult pkgResults;
Microsoft.SqlServer.Dts.Runtime.Variables myVars = pkg.Variables;
pkgLocation =
@"C:\Users\Visual Studio 2008\Projects" +
@"\Integration Services Project1\xyz.dtsx";
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadPackage(pkgLocation, null);
myVars["Account_number"].Value = theText;
pkgResults = pkg.Execute(null, myVars,null,null,null);
}
这里的问题是,当在 window 表单中输入一位数字,比如 9 时,它将开始执行 package.Here 我实际上想让用户输入完整的 account_Number 和 运行 单击按钮时的包。
请告诉我代码中的问题是什么,或者我应该添加什么才能在单击按钮时获得 account_number?
您正在 textBox1_TextChanged
事件中编写代码,这就是每次您在文本框中键入字母或数字时代码都会执行的原因。每次你在文本框中输入内容时 TextChanged
事件都会被触发。
相反,如果您希望代码在 button_click
上执行,那么您应该在 button_click
事件中编写代码。
调用包 button_click。这样,每次您在文本框中输入内容时,都不会执行该包。
编辑
您的 pkg
对象为空,这就是您收到该错误的原因。加载包后移动 Microsoft.SqlServer.Dts.Runtime.Variables myVars = pkg.Variables;
部分。
pkg = app.LoadPackage(pkgLocation, null);
Microsoft.SqlServer.Dts.Runtime.Variables myVars = pkg.Variables;
private void button_clicked(object sender, EventArgs e)
{
string theText = textBox1.Text;
string pkgLocation;
Package pkg = null;
Microsoft.SqlServer.Dts.Runtime.Application app;
DTSExecResult pkgResults;
pkgLocation =
@"C:\Users\Visual Studio 2008\Projects" +
@"\Integration Services Project1\xyz.dtsx";
app = new Microsoft.SqlServer.Dts.Runtime.Application();
pkg = app.LoadPackage(pkgLocation, null);
Microsoft.SqlServer.Dts.Runtime.Variables myVars = pkg.Variables;
myVars["Account_number"].Value = theText;
pkgResults = pkg.Execute(null, myVars,null,null,null);
}