ConnectionString loses password after connection.Open(ConnectionString 连接后丢失密码.打开)
问题描述
我正在使用 ADO.NET 从服务器上的数据库中获取一些信息,
所以这就是我所做的:
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=myServerSQLEXPRESS;Initial Catalog=DBName;User ID=myUser;Password=myPassword";
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
// do stuff
conn.Close();
但在调用 Open 方法后,我注意到 conn.ConnectionString 正在丢失密码,因此它变为:
but after calling Open method i noticed that conn.ConnectionString is losing the password so it becomes:
"Data Source=myServerSQLEXPRESS;Initial Catalog=DBName;User ID=myUser;"
这会导致任何 SqlCommand 后缀异常
如何解决这个问题?
注意:奇怪的是这并不总是发生
我认为它与它自己的命令没有任何关系,但无论如何
which causes exception with any SqlCommand afterwords
how to fix this?
Note:The strange thing is that does not happen always
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();
推荐答案
出于安全原因,这是设计使然.来自 MSDN:
This is by design, for security reasons. From MSDN:
ConnectionString 类似于 OLE DB 连接字符串,但不相同.与 OLE DB 或 ADO 不同,返回的连接字符串与用户设置的 ConnectionString 相同,如果 Persist Security Info 值设置为 false(默认值),则减去安全信息.除非您将 Persist Security Info 设置为 true,否则用于 SQL Server 的 .NET Framework 数据提供程序不会保留或返回连接字符串中的密码.
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 连接后丢失密码.打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ConnectionString 连接后丢失密码.打开


- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 使用 rss + c# 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01