将ConnectionString从SqlConnectionSringBuilder传递到ConnectionManager

时间:2011-11-24 09:26:30

标签: c# package connection-string sqlconnection

我想从ConnectionString的{​​{1}}实例中获取SqlConnectionStringBuilder

这看起来很简单,应该像这样简单:

String

String conString = builder.ConnectionString; 放弃的String并未包含SqlConnectionStringBuilder字段/值。我猜这是某种安全功能,有没有办法强制Password加入Password


进一步观察我认为这可能与String有关。我要做的是修改ConnectionManager的{​​{1}},更改ConnectionString

以下是我的代码,Package的连接字符串被传回Initial Catalog的密码丢失了......

builder

2 个答案:

答案 0 :(得分:2)

在SqlConnectionStringBuilder中设置其他属性之前将PersistSecurityInfo设置为True:D

答案 1 :(得分:0)

我需要稍微重命名一下,因为问题现在不同了,但这是我的解决方案:

@ madd0正确回答了这个问题的第一部分。 ConnectionString确实包含Password字段。


第二部分解决了格式化代码如下:

public void DataTransfer(String sourceConnection, String destConnection, String pkgLocation)
{     
    Package pkg;
    Application app;
    DTSExecResult pkgResults;

    try
    {
        app = new Application();
        pkg = app.LoadPackage(pkgLocation, null);

        foreach (ConnectionManager connectionManager in pkg.Connections)
        {
            SqlConnectionStringBuilder builder;
            switch (connectionManager.Name)
            {
                case "SourceConnection":
                    builder = new SqlConnectionStringBuilder(sourceConnection) { PersistSecurityInfo = true };
                    builder.Remove("Initial Catalog");
                    builder.Add("Initial Catalog", "StagingArea");
                    var sourceCon = builder.ConnectionString + ";Provider=SQLNCLI;Auto Translate=false;";
                    //Added spaces to retain password!!!
                    sourceCon = sourceCon.Replace(";", "; ");
                    connectionManager.ConnectionString = sourceCon;
                    Debug.WriteLine(connectionManager.ConnectionString.ToString());
                    break;
                case "DestinationConnection":
                    builder = new SqlConnectionStringBuilder(sourceConnection) { PersistSecurityInfo = true };
                    builder.Remove("Initial Catalog");
                    builder.Add("Initial Catalog", "StagingArea");
                    var destCon = builder.ConnectionString + ";Provider=SQLNCLI;Auto Translate=false;";
                    //Added spaces to retain password!!!
                    destCon = destCon.Replace(";", "; ");
                    connectionManager.ConnectionString = destCon;
                    Debug.WriteLine(connectionManager.ConnectionString.ToString());
                    break;
            }
        }
        pkgResults = pkg.Execute();
    }
    catch (Exception e)
    {

        throw;
    }
    Console.WriteLine(pkgResults.ToString());
}

我使用ConnectionString玩了一段时间后注意到原来的String在每个属性之间都有空格。

运行2次测试我发现没有空格Password丢失了......

connectionManager.ConnectionString = destCon;

测试1:没有空格:

destCon = Data Source=xxx.xxx.xxx.xxx;Initial Catalog=StagingArea;User ID=*****;Password=*****;Provider=SQLNCLI;Auto Translate=false;

然后connectionManager.ConnectionString = Data Source=xxx.xxx.xxx.xxx;User ID=*****;Initial Catalog=StagingArea;Provider=SQLNCLI;Auto Translate=false;

测试2:空格:

destCon = Data Source=xxx.xxx.xxx.xxx; Initial Catalog=StagingArea; User ID=*****; Password=*****; Provider=SQLNCLI; Auto Translate=false;

然后connectionManager.ConnectionString = Data Source=xxx.xxx.xxx.xxx; Initial Catalog=StagingArea; User ID=*****; Password=*****; Provider=SQLNCLI; Auto Translate=false;

不知道为什么会发生这种情况,但如果没有空格,则会调整排序并丢失Password字段。