0
votes

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. 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).

To connect, I enabled TCP/IP in Configuration Manager on my laptop and added a new rule for the firewall to allow the connection. 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.

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 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 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.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.

1
Why don't you try to catch that exception? - Noisy88
@Noisy88 Thanks for your reply - I'm not really sure how/where to catch the exception? I'm not connecting to the database with my code yet - sorry if that wasn't clear, I've edited the question. Ideally, all I'd like to do for now is be able to view the DB in SQL Server Object Explorer on my desktop, to verify that I can access it. - Thea
A timeout can mean a number of things: 1) there is no network route between the two machines (cable unplugged, no LAN, no DNS, no DHCP, etc) 2) there is no database server running at the given address, 3) the database server is not listening for incoming connections at the given port, or 4) a firewall is still blocking your connection. - Mike Nakis
@MikeNakis thank you - this is probably a silly question (I'm a big noob), but do I need to connect my laptop and desktop explicity (using a LAN cable or something else), or is the fact that they share the same internet connection enough? I've been using this tutorial: docs.microsoft.com/en-us/sql/relational-databases/…, but haven't done any previous setup, so maybe I'm just missing something really obvious? - Thea
The fact that they share the same internet connection is not enough, but the fact that they are probably connected to the same router (via WiFi or Ethernet) should make up for this. Still, you need to make sure that they see each other. I would suggest that you first try to troubleshoot the connection between the two irrespective of the database (what does NET VIEW show? what does NET VIEW \\SERVER show?) and once you have established that this works, then try the more complicated situation of getting one to see a database on the other. - Mike Nakis

1 Answers

0
votes

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;
    }
}