how to connect Remote SQL Server Database . through the internet using Public Static IP and Port Number..?

NazHim 201 Reputation points
2021-08-05T18:39:41.747+00:00

Hi All,
how to connect Remote SQL Server Database. through the internet using Public Static IP and Port Number..?
in remote machine. i am used SQL Server 2019.
i want to connect remote SQL Server Database. with using public static IP 126.158.2.125 and Port 50121.
from Local Machine.
i am created port with number 50121.
created firewall Rules also. like image at below.

in Windows defender firewall
TCP Inbound Rules.
120885-2021-08-05-213729-01.png
120830-2021-08-05-213856-02.png
120894-2021-08-05-213926-03.png
120921-2021-08-05-214200-04.png
120856-2021-08-05-220952-ib-05.png

UDP Outbound Rules.
120932-2021-08-05-220358-ob-01.png
120857-2021-08-05-220451-ob-02.png
120895-2021-08-05-220533-ob-03.png
120896-2021-08-05-220607-ob-04.png
120897-2021-08-05-220734-ob-05.png

Connection Local:
it's working fine.
used Connection string like at below.
Server Local Machine IP: 192.168.1.17 (sample)
Port Number: 50121.

SqlConnectionStringBuilder sqlConBui = new SqlConnectionStringBuilder();  
sqlConBui.DataSource = @"192.168.1.17,50121\STUDENTMSSQLSERVER";  
sqlConBui.InitialCatalog = "StudentDatBas";  
sqlConBui.PersistSecurityInfo = true;  
sqlConBui.MultipleActiveResultSets = true;  
sqlConBui.UserID = "Student";  
sqlConBui.Password = "Stu1234";  
string connectionString = sqlConBui.ConnectionString;  
try  
{  
    SqlConnection con = new SqlConnection(connectionString);  
    con.Open();  // Open the connection  
}  
catch (Exception ex)  
{  
    MessageBox.Show(ex.Message);  
}  

i want to Access SQL Server DataBase from Remote machine. through the internet.
with using public static IP Address and specific port number.

Remote Connection:
it's not working.
used Connection string like at below.
Public Static IP: 126.158.2.125 (sample)
Port Number: 50121.

SqlConnectionStringBuilder sqlConBui = new SqlConnectionStringBuilder();  
    sqlConBui.DataSource = @"126.158.2.125,50121\STUDENTMSSQLSERVER";  
    sqlConBui.InitialCatalog = "StudentDatBas";  
    sqlConBui.PersistSecurityInfo = true;  
    sqlConBui.MultipleActiveResultSets = true;  
    sqlConBui.UserID = "Student";  
    sqlConBui.Password = "Stu1234";  
    string connectionString = sqlConBui.ConnectionString;  
    try  
    {  
        SqlConnection con = new SqlConnection(connectionString);  
        con.Open();  // Open the connection  
    }  
    catch (Exception ex)  
    {  
        MessageBox.Show(ex.Message);  
    }  

getting error.
like image at below.

120916-2021-08-05-224729-er-01.png

Port Result:
120972-2021-08-06-000829-pr-01.png

SQL Server Image:
120838-2021-08-05-225259-sql-server-01.png
120900-2021-08-05-225410-sql-server-02.png
120839-2021-08-05-225541-sql-server-03.png

SQL Server Configuration Manager:
120952-2021-08-05-230306-scm-01.png
120924-2021-08-05-230442-scm-02.png
120903-2021-08-05-230810-scm-03.png
120877-2021-08-05-230920-scm-04.png
120850-2021-08-05-231113-scm-05.png

how i can do it..?.
can provie some code snippet..?.

with best Regards.
NazHim

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,690 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
{count} votes

5 answers

Sort by: Most helpful
  1. Cris Zhan-MSFT 6,601 Reputation points
    2021-08-06T02:39:13.853+00:00

    Hi,

    > sqlConBui.DataSource = @"126.158.2.125,50121\STUDENTMSSQLSERVER";

    Please try "126.158.2.125,50121"

    >network-related or instance-specific error occurred while establishing...

    This error usually means that the client can't find the SQL Server instance.

    If you cannot connect SQL Server through your application, please confirm whether the network and port is work fine, and try to use SSMS to remotely connect to the SQL Server instance for testing to explore where the problem may be.
    https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/troubleshoot-connecting-to-the-sql-server-database-engine?view=sql-server-ver15

    0 comments No comments

  2. NazHim 201 Reputation points
    2021-08-06T04:59:37.113+00:00

    hello Cris Zhan-MSFT
    thanks for your fast response.

    i am tried. 126.158.2.125,50121 (IP is sample not real)
    bad luck. not working it.

    regards
    NazHim


  3. Bruce (SqlWork.com) 55,601 Reputation points
    2021-08-09T19:21:46.553+00:00

    you opened the firewall on the sqlserver windows box, but did you enable port forwarding from its modem?

    0 comments No comments

  4. Bonnie DeWitt 811 Reputation points
    2021-08-09T19:58:57.633+00:00

    Here is a sample connection string, @NazHim :

    "server=TheIPAddress;uid=TheUser;pwd=ThePassword;database=TheDatabase"  
    

    Replace your values in the string.

    0 comments No comments

  5. NazHim 201 Reputation points
    2021-08-10T03:26:08.403+00:00

    Hello BonnieDeWitt-QnA

    i am using C# windows form application

    regards
    NazHim