3

以下是我使用 Microsoft OLE DB Provider for SQL Server 的配置工具连接到数据库时生成的连接字符串。

Provider=SQLOLEDB.1;Password=password;Persist Security Info=True;User ID=sa;
Initial Catalog=database;Data Source=localhost;Use Procedure for Prepare=1;
Auto Translate=True;Packet Size=4096;Workstation ID=computer1;
Use Encryption for Data=False;Tag with column collation when possible=False

如果我使用 SQL Server Native Client 10.0 连接到同一个数据库,我会得到这个连接字符串。

Provider=SQLNCLI10.1;Integrated Security=\"\";Persist Security Info=False;
User ID=sa;Initial Catalog=database;Data Source=localhost;Use Procedure for Prepare=1;
Auto Translate=True;Packet Size=4096;Workstation ID=computer1;
Initial File Name=\"\";Use Encryption for Data=False;
Tag with column collation when possible=False;
MARS Connection=False;DataTypeCompatibility=0;Trust Server Certificate=False\0

我有 ac# 应用程序,它读取这些连接字符串之一并使用它来创建与我的数据库的连接,如下所示

SqlConnectionStringBuilder _sqlConnectionStringBuilder = new SqlConnectionStringBuilder();
OleDbConnectionStringBuilder conBuilder = new OleDbConnectionStringBuilder( CONNECTION STRING SHOWN ABOVE);
_initialCatalogValue = (string)conBuilder["Initial Catalog"];
_dataSourceValue = conBuilder.DataSource;

_sqlConnectionStringBuilder.Password = (string)conBuilder["Password"];
_sqlConnectionStringBuilder.UserID = (string)conBuilder["User Id"];
_sqlConnectionStringBuilder.InitialCatalog = _initialCatalogValue;
_sqlConnectionStringBuilder.DataSource = _dataSourceValue;
_sqlConnectionStringBuilder.PersistSecurityInfo = true;

_conn = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString);
_conn.Open();

当我使用 SQL Server 本地客户端时密码为空并且我的 SQLConnection 将在登录时失败的问题。我得到的错误是“用户'sa'登录失败”。

OLE DB 连接字符串成功。由于我在 c# 中使用的某些类,我的 sql server 本机客户端登录失败了吗?Sql Server Native Client 10.0 是否加密密码?我应该尝试识别连接字符串中的哪个提供程序并且有两个不同的代码路径吗?如果是这样,连接需要什么?

基本问题是,无论我收到哪个连接字符串(仅以上两个),如何确保连接成功?

注意我无法控制连接字符串。我只能使用我收到的东西。

4

2 回答 2

1

您提供的第二个连接字符串包含密码;因此,当您设置为第二个字符串conBuilder["Password"]时返回一个空字符串。conBuilder.ConnectionString

于 2013-01-28T15:27:06.963 回答
0

问题解决如下。

有一个主应用程序正在使用上述连接字符串并执行以下操作。

应用程序将采用连接字符串。如果连接字符串的提供程序是 SQL Server Native Client (SQLNCLI10.1),则应用程序会检查持久安全性。如果找不到任何内容,则将 IntegratedSecurity=SSPI 添加到连接字符串,然后使用 Windows 身份验证进行连接。这是否是正确的(或安全的事情),这就是正在做的事情。

“回答”这个问题。您可以输入连接字符串,如果找不到密码,您可以设置 IntegratedSecurity=SSPI。这将允许您使用 Windows 身份验证而不是 SQL Server 身份验证进行连接。我不建议你这样做,但它会起作用。

于 2013-01-29T13:40:02.673 回答