我正在创建一个SSIS包,并希望包含一个脚本,该脚本在检索文件并将该数据保存到表之前检查文件是否存在。
我已经设置了三个单独的变量:
fileExistFlag Int32 0
fileName String check.txt
folderPath String C:\
我的C#代码如下所示,我正在检查:
public void Main()
{
// TODO: Add your code here
String fp = Dts.Variables["User::folderPath"].Value.ToString() + Dts.Variables["User::fileName"].Value.ToString();
if (File.Exists(fp))
{
Dts.Variables["User::fileExistFlag"].Value = 1;
}
MessageBox.Show(fp);
MessageBox.Show(Dts.Variables["User::fileExistFlag"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
当我尝试编译脚本时,收到以下错误:
所有四个实例都 Cannot apply indexing with [] to an expression of type 'Microsoft.SqlServer.Dts.Runtime.Variables
。
我该如何解决这个问题?
更新的代码:
/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
namespace ST_04f6fa3ba49a4ddeac3d3d7fc29f04f2.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
/*
The execution engine calls this method when the task executes.
To access the object model, use the Dts property. Connections, variables, events,
and logging features are available as members of the Dts property as shown in the following examples.
To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
To post a log entry, call Dts.Log("This is my log text", 999, null);
To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);
To use the connections collection use something like the following:
ConnectionManager cm = Dts.Connections.Add("OLEDB");
cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";
Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
To open Help, press F1.
*/
public void Main()
{
// TODO: Add your code here
String fp = Dts.Variables.Get("User::folderPath").Value.ToString() + Dts.Variables.Get("User::fileName").Value.ToString();
if (File.Exists(fp))
{
Dts.Variables.Get("User::fileExistFlag").Value = 1;
}
MessageBox.Show(fp);
MessageBox.Show(Dts.Variables.Get("User::fileExistFlag").Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
}
public static Microsoft.SqlServer.Dts.Runtime.Variable Get(
this Microsoft.SqlServer.Dts.Runtime.Variables variables, string name)
{
foreach(Microsoft.SqlServer.Dts.Runtime.Variable item in variables)
{
if(item.Name == name) return item;
}
return null;
}
}
答案 0 :(得分:13)
这是SQL Server BIDS 2005/2008中已知的BUG,并排安装了更高版本的SSIS。例如,如果您正在开发SSIS 2008包,然后安装SSIS 2012。
解决方法是移动位于路径中的文件" Microsoft.SQLServer.ManagedDTS.dll" : " C:\ Program Files(x86)\ Microsoft SQL Server \ 110 \ SDK \ Assemblies" 到备份文件夹,然后出价从路径&获取参考#34; C:\的Windows \组件\ GAC_MSIL \ Microsoft.SqlServer.ManagedDTS \ 10.0.0.0__89845dcd8080cc91 \"
但它似乎并不适用于报告的所有病例。
来源:
答案 1 :(得分:3)
奇怪的是,这个索引器does seem to exist。但是,如果它不起作用,您可以使用扩展方法:
public static class MyExtensionMethods
{
public static Microsoft.SqlServer.Dts.Runtime.Variable Get(
this Microsoft.SqlServer.Dts.Runtime.Variables variables, string name)
{
foreach(Microsoft.SqlServer.Dts.Runtime.Variable item in variables)
{
if(item.Name == name) return item;
}
return null;
}
}
并使用:
... Dts.Variables.Get("User::folderPath").Value ...
代替。
答案 2 :(得分:1)
在“添加引用”窗口中使用“浏览”并查找此dll:C:\ Program Files(x86)\ Microsoft SQL Server \ 100 \ SDK \ Assemblies \ Microsoft.SQLServer.ManagedDTS.dll