【问题标题】:Connection string for SQL Server 2008 R2 Versus for SQL Server ExpressSQL Server 2008 R2 与 SQL Server Express 的连接字符串
【发布时间】:2014-05-15 01:33:48
【问题描述】:

我正在安装一个开源工具,在定义数据库设置时我需要指定数据库连接字符串。它们为 SQL Server Express 的连接字符串提供了一个示例,如下所示:

Database=roadkill;Server=.\SQLEXPRESS;uid=username;pwd=password;

但由于我使用的是 SQL Server 2008 R2 而不是 SQL Server Express,如何以同样的方式为我的 SQL Server 2008 R2 定义连接字符串?

谢谢

编辑

我尝试了以下连接字符串:-

Data Source=.\SQLExpress;Integrated Security=true; AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\roadkill.mdf

但我遇到了以下异常:-

System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)

【问题讨论】:

  • 这是ConnectionStrings-Я-US 网站:-)
  • 有一个 Sql Server 2008 R2 的快捷版。它们是(或可以是)一回事。

标签: sql sql-server sql-server-2008


【解决方案1】:

SQL Server 和 SQL Server Express 的连接字符串格式完全相同。在这两种情况下,ServerData Source 值都必须是服务器和实例名称。 ".\SQLEXPRESS" 仅表示本地计算机上名为 "SQLEXPRESS" 的实例。 SQL Server Express 使用该实例名称是惯例,但这不是必需的。您可以使用不同的名称,也可以将该名称用于完整的 SQL Server 实例,尽管这会产生误导。它只是一个识别标签。在所有情况下,对命名实例使用“ServerName\InstanceName”,对默认实例使用“ServerName”。您可以使用 ”。”或 "(local)" 表示本地机器。

【讨论】:

  • 该错误消息表明本地计算机上没有名为“SQLEXPRESS”的实例。
  • 顺便说一句,您为什么要附加一个似乎位于表明它已经或应该已经永久附加的位置的文件?
【解决方案2】:

这两个版本的sqlserver没有区别。唯一的区别在于 sqlexpress 中的服务器参数写成“.SQLEXPRESS”或者说(LOCAL)这两种情况。

数据库=".Sqlexpress"
或者 数据库=“(本地)”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    相关资源
    最近更新 更多