Search code examples
c#sql-servertcp

how to connect to remote SQL Server Express over internet?


I have a Windows 10 PC with SQL Server Express, I have setup remote connections, allocated a fixed port (express, 22172, removed TCP Dynamic Ports under IPALL), set an inbound rule for the port etc.

I can connect and manipulate the database from a Linux PC on the same network (on the same LAN router), using:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = @"192.168.1.22\SQLEXPRESS, 22172";
builder.InitialCatalog = "<database>";
builder.ItegratedSecurity=  "sa";
builder.Password = "<password>"

string connectionString = builder.ConnectionString;

So, the normal firewall inbound rules, remote connection settings etc work, correct? I now want to be able to make that same connection, from a Windows PC that is on a different internet (from a different location/town..)

I have:

  • Acquired a fixed IP for the router that the host PC is on
  • Fixed the host PC's local IP - 192.168.1.22 according to this link
  • Set the IP under IP2 for "Protocols for SQLEXPRESS - TCP/IP - IP Addresses" to 192.168.1.22
  • Added SQL Server Windows NT - 64 Bit as allowed app in firewall
  • Enabled TCP/IP and Named Pipes in SQL Server Client Network Utility with Start - Run - CLICONFG.exe
  • Set port forwarding on the router: Service Port 22172, IP Address: 192.168.1.22, Internal Port 22172, Protocol - TCP, Enabled

I have switched off the firewall, and still get the error: "(provider: TCP Provider, error 40 - Could not open a connection to SQL Server)

I try these connection string options:

        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.DataSource = @"tcp:xxx.xxx.x.xx\SQLEXPRESS, 22172";
        builder.InitialCatalog = "<database>";
        builder.UserID = "sa";
        builder.IntegratedSecurity = false;
        builder.Password = "<password>";

        sqlConnect = builder.ConnectionString;

I have also tried these variations:

        builder.DataSource = @"x.xxx.x.xx\SQLEXPRESS, 22172";
        builder.DataSource = @"tcp:xxx.xxx.x.xx, 22172";
        builder.DataSource = @"xxx.xxx.x.xx, 22172";

Also these connection strings:

        sqlConnect = @"Data Source = tcp:xxx.xxx.x.xx\SQLEXPRESS,22172; Initial Catalog = <database>; User ID = sa; Password = <password>";
        sqlConnect = @"Data Source = xxx.xxx.x.xx\SQLEXPRESS,22172; Initial Catalog = <database>; User ID = sa; Password = <password>";
        sqlConnect = @"Data Source = tcp:xxx.xxx.x.xx,22172; Initial Catalog = <database>; User ID = sa; Password = <password>";
        sqlConnect = @"Data Source = xxx.xxx.x.xx,22172; Initial Catalog = <database>; User ID = sa; Password = <password>";

And for good measure, this one:

sqlConnect = @"Data Source=xxx.xxx.x.xx\SQLEXPRESS,22172;Initial Catalog=<database>;Persist Security Info=True;User ID=sa;Password=<password>;";

I have scanned all relevant questions on SO etc - somewhere I am doing something small wrong? Please help

Output when running select * from sys.tcp_endpoints on host PC SQL: Thus, ip_address is NULL for both, port is 0 for both, and is_dynamic_port is 1 for both - is that the relevant information?

Dedicated Admin Connection  1   1   2   TCP 2   TSQL    0   STARTED 1   0   1   NULL
TSQL Default TCP            4   1   2   TCP 2   TSQL    0   STARTED 0   0   1   NULL

I have tried several times to change the dynamic port to static, I have removed all 0 or port numbers in configuration manager, but still get a 1 for _is_dynamic_port. Event Viewer shows:

Server is listening on [ 'any' <ipv4> 22172].

Same message if I run the following in SQL:

EXEC xp_ReadErrorLog 0, 1, N'Server is listening on', N'any', NULL, NULL, 'DESC'

GO

Why does SQL settings remain on dynamic ports?


Solution

  • Check that your ISP does not block ports / all ports. That is what stopped me.