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 连接后丢失密码.打开
基础教程推荐
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
