我正在安装一个开源工具,在定义数据库设置时,我需要指定数据库连接字符串。它们提供了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)
答案 0 :(得分:3)
SQL Server和SQL Server Express的连接字符串格式完全相同。在这两种情况下,Server
或Data Source
值必须是服务器和实例名称。 “。\ SQLEXPRESS”仅表示本地计算机上名为“SQLEXPRESS”的实例。惯例是,该实例名称用于SQL Server Express,但不是必需的。您可以使用其他名称,也可以将该名称用于完整的SQL Server实例,但这会产生误导。它只是一个识别标签。在所有情况下,对于命名实例使用“ServerName \ InstanceName”,对于默认实例使用“ServerName”。您可以使用 ”。”或“(本地)”指代本地机器。
答案 1 :(得分:1)
这两个版本的sqlserver没有区别。对于两种情况,只有sqlexpress的服务器参数写入“.SQLEXPRESS”或说(LOCAL)。
数据库= “SQLEXPRESS”
要么
数据库= “(本地)”