在安装了带有命名实例的SQL Server的Windows 2008 R2独立环境中,SqlConnection失败:
SqlConnection myConnection = new SqlConnection(
"user id=domain/username;password=pa$$word;server=MyServername/MyInstanceName;" +
"Trusted_Connection=yes;database=MyDatabase2;connection timeout=30");
我可以使用Visual Studio的'Server Explorer'成功连接到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
谢谢!
答案 0 :(得分:0)
对于本地实例,请更好地使用“。”而不是服务器名称。例如:
string constr="server=.\<instance-name>;integrated security=true;database=<db-name>";
检查是否有效。
答案 1 :(得分:0)
如果您想通过用户/密码连接到sql-server而不是使用集成安全性
您的代码是否在网络应用中运行?如果是这样,iis-default-user可能没有运行服务器的Windows机器的访问权限