SQL 服务器本机客户端的连接字符串 ASP.NET web.config

Connection string for SQL Server native client in ASP.NET web.config

我想从我的 ASP.NET 应用程序使用 SQL 服务器本机客户端连接到 SQL Server 2012。目前,我有一个现有的连接字符串使用 odbc 连接并且工作正常。

<appSettings>
    <add key="StagingConnect" 
         value="Integrated Security=True;Initial Catalog=Staging;Data Source=AUBDSG01.AUYA.NET\INST1"/>
</appSettings>

当我尝试如下时,代码抛出异常

<add key="StagingConnect"  
     value="Provider=SQLNCLI11;Integrated Security=True;Initial Catalog=Staging;Data Source=AUBDSG01.AUYA.NET\INST1"/>

异常:

System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown.

System.ArgumentException: Keyword not supported: 'provider'.
at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey)
at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules)
at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
at System.Data.ProviderBase.DbConnectionFactory

如何修改此连接字符串,使其通过 SQL 服务器本机客户端 11

连接

不确定您之前是如何使用它的,因为我的连接字符串不在 <appSettings> 中,而是在单独的 <connectionStrings> 部分中。并且 providerName 是一个元素,而不是字符串本身的一部分。

这是一个例子

  <connectionStrings>
    <clear />
    <add name="xxx" providerName="System.Data.SqlClient" connectionString="Server=(local);Database=yyy;User=zzz;Password=123;MultipleActiveResultSets=True" />
  </connectionStrings>

希望对您有所帮助。

根据 MSDN 使用 Database.OpenConnectionString Method (String, String):

var connectionString = "Data Source=.\SQLExpress;Initial Catalog=SmallBakery;Integrated Security=True";

var providerName = "System.Data.SqlClient";

var db = Database.OpenConnectionString(connectionString, providerName);

var selectQueryString = "SELECT * FROM Product ORDER BY Name";

web.config中添加此设置:

<add name="ConnString" connectionString="Password=Secret;Persist Security Info=True;
     User ID=MyUserID;Initial Catalog=SmallBakery;Data Source=.\SQLExpress" 
     providerName="System.Data.SqlClient" />

第一个连接字符串不是 ODBC 连接字符串,它是 SqlClient 连接字符串。

第二个连接字符串是一个 Ole Db 连接字符串,它使用 SQL Server Native Client。但是您的堆栈跟踪显示您正在使用 SqlClient 连接到 SQL 服务器。

您不能同时使用 SqlClient 和 SQL Native client 连接到 SQL Server。要使用 Native Client,您有两个选择:

您可以按照以下 Microsoft 支持文章使用 SqlClient 和 TLS1.2:

您可以在 web.config 中对此进行测试。这需要您显示的用户名和密码。不过,我不确定您将如何使用连接字符串。如果使用的帐户具有访问权限,这应该可以工作。

<?xml version="1.0" encoding="utf-8"?>
<configuration>

</configSections>
<connectionStrings>
    <add name="StagingConnect" connectionString="data source=AUBDSG01.AUYA.NET\INST1;initial catalog=Staging;persist security info=True;user id=user;password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>

删除连接字符串中的 Provider=SQLNCLI11 部分 -- 它不受支持 属性 并且没有必要。

参考:MSDN

如果您使用的是 SqlConnection,请使用提供程序名称 "System.Data.SqlClient",但如果您想使用其他提供程序

SqlConnection

<add key="StagingConnect" 
         value="Integrated Security=True;Initial Catalog=SampleDatabase;Data Source=."/>

OleDbConnection

 <add key="StagingConnect2" 
         value="Provider=SQLNCLI11;Server=.;Database=SampleDatabase;
Trusted_Connection=yes;"/>

OdbcConnection

 <add key="StagingConnect3" 
         value="Driver={SQL Server Native Client 11.0};Server=.;
Database=SampleDatabase;Trusted_Connection=yes;"/>

我测试了所有这些并且有效fine.i希望这会有所帮助

我认为您的连接问题是您在使用 windows 身份验证

时无法远程连接到 SQL

你可以这样试试:

<appSettings>
<add key="StagingConnect" 
     value="data source=AUBDSG01.AUYA.NET\INST1;initial catalog=Staging;persist security info=True;user id=username;password=password;MultipleActiveResultSets=True"/></appSettings>