ConnectionString 连接后丢失密码.打开

ConnectionString loses password after connection.Open(ConnectionString 连接后丢失密码.打开)
本文介绍了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 连接后丢失密码.打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)