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 连接后丢失密码.打开


基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01