c# – ConnectionString在连接后丢失密码 打开

我使用ADO.NET从服务器上的数据库获取一些信息,
所以这是我做的:

string conStr = "Data Source=myServer\SQLEXPRESS;Initial Catalog=DBName;User ID=myUser;Password=myPassword";

SqlConnection conn = new SqlConnection(conStr);

conn.Open();
// do stuff
conn.Close();

但是在调用Open方法之后,我注意到conn.ConnectionString正在丢失密码,所以它变成:

"Data Source=myServer\SQLEXPRESS;Initial Catalog=DBName;User ID=myUser;"

这导致任何SqlCommand后缀异常
如何解决这个问题?
注意:奇怪的是,这并不总是发生
编辑:我不认为它与命令有关,但无论如何

SqlCommand command = new SqlCommand("select GetDate()", conn);
SqlDataReader reader = command.ExecuteReader();
出于安全考虑,这是设计的。从MSDN

The ConnectionString is similar to an OLE DB connection string, but is not identical. Unlike OLE DB or ADO, the connection string that is returned is the same as the user-set ConnectionString, minus security information if the Persist Security Info value is set to false (default). The .NET Framework Data Provider for SQL Server does not persist or return the password in a connection string unless you set Persist Security Info to true.

翻译自:https://stackoverflow.com/questions/12467335/connectionstring-loses-password-after-connection-open

转载注明原文:c# – ConnectionString在连接后丢失密码 打开