我想从ConnectionString
的{{1}}实例中获取SqlConnectionStringBuilder
。
这看起来很简单,应该像这样简单:
String
但String conString = builder.ConnectionString;
放弃的String
并未包含SqlConnectionStringBuilder
字段/值。我猜这是某种安全功能,有没有办法强制Password
加入Password
?
进一步观察我认为这可能与String
有关。我要做的是修改ConnectionManager
的{{1}},更改ConnectionString
。
以下是我的代码,Package
的连接字符串被传回Initial Catalog
的密码丢失了......
builder
答案 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
字段。