SSIS:如何通过脚本任务设置初始目录的值

时间:2011-09-09 23:02:11

标签: c# sql-server-2005 ssis

我正在循环我的联系:

for (int x = 0; x < Dts.Connections.Count; x++)
{

    switch (Dts.Connections[x].Name.ToString())
    {
        case "m":

           for (int z = 0; z < Dts.Connections[x].Properties.Count; z++)
            {
                      if ( Dts.Connections[x].Properties[n].Name = "Initial Catalog"){
                Dts.Connections[x].Properties[n].SetValue(object o, object value);}
            }

            break;
    }                  
}

以上就我所知,setvalue的签名是(Object o,Object value)

2 个答案:

答案 0 :(得分:0)

尝试使用以下方法设置DTS连接

ConnectionManager cn = Dts.Connections.Add("OLEDB");
cn.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";

答案 1 :(得分:0)

根据the documentation of SetValue(),第一个参数是您要在其上设置属性的对象,第二个参数是属性值。所以它应该是这样的:

for (int i = 0; i < Dts.Connections.Count; i++)
{
    var connection = Dts.Connections[i];
    if (connection.Name == "m")
    {
        for (int j = 0; j < connection.Properties.Count; j++)
        {
            var property = connection.Properties[j];

            if (property.Name == "Initial Catalog")
                property.SetValue(connection, "some value");
        }
    }
}

或者使用LINQ:

var connection = Dts.Connections.Cast<ConnectionManager>()
                                .First(cm => cm.Name == "m");
var property = connection.Properties.Cast<DtsProperty>()
                                    .Single(p => p.Name == "Initial Catalog");
property.SetValue(connection, "some value");

Cast()是必要的,因为遗憾的是,这些集合不是通用的。)