ConnectionString的connection.Open后丢失的密码 [英] ConnectionString loses password after connection.Open

查看:608
本文介绍了ConnectionString的connection.Open后丢失的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 字符串构造=数据源= MYSERVER \ SQLEX $ P $干燥综合征;初始目录=数据库名;用户ID = MYUSER;密码= MYPASSWORD;

SqlConnection的康恩=新的SqlConnection(构造);

conn.Open();
//做的东西
conn.Close();
 

但调用打开方式,我注意到在那之后的 conn.ConnectionString 正在失去的密码,这样就变成:

 数据源= MYSERVER \ SQLEX $ P $干燥综合征;初始目录=数据库名;用户ID = MYUSER;
 

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

 的SqlCommand命令=新的SqlCommand(选择GETDATE(),康涅狄格州);
SqlDataReader的读卡器= Command.ExecuteReader却();
 

解决方案

这是由设计,出于安全考虑。从<一个href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx">MSDN:

  

ConnectionString中类似于OLE DB连接字符串,但不完全相同。与OLE DB或ADO,返回的是相同的用户设置的ConnectionString,减去的安全信息,如果坚持安全信息的值设置为false(默认值)的连接字符串。 .NET Framework数据提供程序的SQL Server不持续存在或除非将坚持安全信息为真返回密码的连接字符串。

i'm using ADO.NET to get some information from the database on a server,
so this is what i do:

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();

but after calling Open method i noticed that conn.ConnectionString is losing the password so it becomes:

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

which causes exception with any SqlCommand afterwords
how to fix this?
Note:The strange thing is that does not happen always
Edit: i don't think it has anything to do with the command it self but anyway

SqlCommand command = new SqlCommand("select GetDate()", conn);
SqlDataReader reader = command.ExecuteReader();

解决方案

This is by design, for security reasons. From 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.

这篇关于ConnectionString的connection.Open后丢失的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆