1

SqlConnection 在使用命名实例安装 SQL Server 的 Windows 2008 R2 独立环境中失败:

        SqlConnection myConnection = new SqlConnection(
            "user id=domain/username;password=pa$$word;server=MyServername/MyInstanceName;" +
            "Trusted_Connection=yes;database=MyDatabase2;connection timeout=30");

我可以使用 Visual Studio 的“服务器资源管理器”成功连接到 SQL Server 实例它创建的连接字符串是:

Data Source= MyServername\MyInstanceName;Initial Catalog=MyDatabase2;Integrated Security=True

在我的 SqlConnection 中,我尝试了以下结果,结果显示...

server=MyServername/MyInstanceName;" +    //fails within timeout
server=MyServername\MyInstanceName;" +    //Unrecognized escape sequence
server=MyServername\\MyInstanceName;" +   //suspends far longer than timeout
server=localhost\\MyInstanceName;" +      //suspends far longer than timeout
server=localhost/MyInstanceName;" +       //fails within timeout

这不是远程连接。VS 和 SQL Server 都在同一台机器上。如何连接到命名实例?

这是我收到的错误消息:

System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-s
pecific 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 a
nd that SQL Server is configured to allow remote connections. (provider: Named P
ipes Provider, error: 40 - Could not open a connection to SQL Server) ---> Syste
m.ComponentModel.Win32Exception (0x80004005): The network name cannot be found
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception
, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObj
ect stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternal
ConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Bool
ean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFa
ilover)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo
serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSn
iOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo
serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirect
edUserInstance, SqlConnectionString connectionOptions, SqlCredential credential,
 TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTime
r timeout, SqlConnectionString connectionOptions, SqlCredential credential, Stri
ng newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdent
ity identity, SqlConnectionString connectionOptions, SqlCredential credential, O
bject providerInfo, String newPassword, SecureString newSecurePassword, Boolean
redirectedUserInstance, SqlConnectionString userConnectionOptions)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOp
tions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConn
ectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)

   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConn
ectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbCon
nectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions
 userOptions)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOp
tions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow
ningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean o
nlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& co
nnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection ow
ningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbCon
nectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection
 owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions
, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection
 outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1
retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at ConsoleApplication1.Program.Main(String[] args) in c:\Users\hptSetupAdmin\
Documents\Visual Studio 2012\Projects\ConsoleApplication1_ConnToSql\ConsoleAppli
cation1\Program.cs:line 33
ClientConnectionId:00000000-0000-0000-0000-000000000000

谢谢!

4

2 回答 2

1

对于本地实例,最好使用“。” 而不是服务器名称。例如:

string constr="server=.\<instance-name>;integrated security=true;database=<db-name>";

检查这是否有效。

于 2013-05-12T06:01:42.650 回答
0

如果您想通过用户/密码而不是使用集成安全连接到 sql-server

  • 您的用户需要对运行服务器的 Windows 机器的访问权限
  • 您的数据库必须配置为通过用户/密码接受登录
  • 您必须授予此用户对 sql-server 和您要使用的数据库的访问权限。

您的代码是否在网络应用程序中运行?如果是这样,iis-default-user 可能没有对运行服务器的 windows 机器的访问权限

于 2013-05-12T06:05:41.027 回答