简体   繁体   English

尝试连接到另一台计算机上存储的数据库时出现“等待操作超时”错误

[英]'The wait operation timed out' error when trying to connect to a database stored on another computer

I'm working on an EF Core project that uses a SQL Server database, which I created on my laptop, and want to access it from my desktop.我正在开发一个使用 SQL 服务器数据库的 EF Core 项目,该数据库是我在笔记本电脑上创建的,并希望从我的桌面访问它。 I'm not accessing it with my code yet - I'd just like to manually connect to it, and be able to see the DB in SQL Server Object Explorer for now (to verify that my desktop is connected to it before running my program).我还没有使用我的代码访问它 - 我只想手动连接到它,并且能够在 SQL 服务器 Object 资源管理器中看到数据库(在运行我的程序之前验证我的桌面是否已连接到它)。

To connect, I enabled TCP/IP in Configuration Manager on my laptop and added a new rule for the firewall to allow the connection.为了连接,我在笔记本电脑的配置管理器中启用了 TCP/IP,并为防火墙添加了一条新规则以允许连接。 Then, when I type my laptop's name into SSMS on my desktop, after about 30 seconds, it throws the 'wait operation timed out' error.然后,当我在桌面上的 SSMS 中输入笔记本电脑的名称时,大约 30 秒后,它会抛出“等待操作超时”错误。

Here is the stack trace:这是堆栈跟踪:

at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) at Sys at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager ) 在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions 选项,DbConnectionPoolKey poolKey,Object poolGroupProviderInfo,DbConnectionPool 池,DbConnection owningConnection,DbConnectionOptions userOptions) 在 System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection DbGroupConnectionOptions userOptions)在系统tem.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) at System.Data.SqlClient.SqlConnection.Open() at Microsoft.SqlServer.Management.SqlStudio.Explorer.ObjectExplorerService.ValidateConnection(UIConnectionInfo ci, IServerType server) at Microsoft.SqlServer.Management tem.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1 重试,DbConnectionOptions userOptions)在 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 重试)在 System.Data.SqlClient.SqlConnection.TryOpen( TaskCompletionSource`1 retry) at System.Data.SqlClient.SqlConnection.Open() at Microsoft.SqlServer.Management.SqlStudio.Explorer.ObjectExplorerService.ValidateConnection(UIConnectionInfo ci, IServerType server) at Microsoft.SqlServer.Management .UI.ConnectionDlg.Connector.ConnectionThreadUser() .UI.ConnectionDlg.Connector.ConnectionThreadUser()

I have very little experience with connecting computers across networks, and only just learned how to create a firewall rule, so my guesses for what the problem could be are limited.我对跨网络连接计算机的经验很少,只是刚刚学会了如何创建防火墙规则,所以我对问题可能是什么的猜测是有限的。 Maybe there's a simpler way to access the database?也许有更简单的方法来访问数据库? Probably not, from what I've seen, but if there is I'd be happy to know!可能不是,从我所看到的,但如果有我会很高兴知道!

Thanks in advance for any help you can offer.提前感谢您提供的任何帮助。

This is a piece of real code.这是一段真实的代码。 I think it will be useful for your case.我认为这对您的情况很有用。

public async Task<bool> CheckMsSqlConnection(string connectionString, int timeout = 8000)
{
    try
    {
        var dc = new MyDbContext(connectionString);
        var ctx = new CancellationTokenSource();
        ctx.CancelAfter(TimeSpan.FromMilliseconds(timeout));

        var db = dc.Database;
        db.CommandTimeout = 1;

        await db.Connection.OpenAsync(ctx.Token);

        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM