Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The right connection string for Remote SQL server for C#

I just want to know the right sql connection string for a remote sql server express edition.

This is what I got but I got some problems

 SqlConnection cs = new SqlConnection(@"Data Source=(IP Address)\PC-NAME\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password");

I got this error in my C# debugger:

A network-related or instance-specific 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 and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)

Thanks in advance!

like image 393
Lucas Juan Avatar asked Dec 13 '13 02:12

Lucas Juan


People also ask

What should be the connection string for SQL Server?

The following connection string will connect the database using windows authentication. Server=ServerName;Database=DatabaseName;Trusted_Connection=True; With help of the following C# code, this is how we can see the usage of a connection string in an application.

How can I set an SQL Server connection string?

You can either use the new operator to make that directly. For example: SqlConnection conn = new SqlConnection( new SqlConnectionStringBuilder () { DataSource = "ServerName", InitialCatalog = "DatabaseName", UserID = "UserName", Password = "UserPassword" }. ConnectionString );

What is the connection URL for SQL Server?

jdbc:sqlserver:// (Required) is known as the subprotocol and is constant. serverName (Optional) is the address of the server to connect to. This address can be a DNS or IP address, or it can be localhost or 127.0. 0.1 for the local computer.


3 Answers

From your comment:

The IP address is the static IP address. I am trying to connect outside the building.

If the database you are trying to connect to is on a computer behind a [router/firewall/modem] that has the target address, you will need to use port forwarding on the [router/firewall/modem] to forward all connections on TCP port 1433 through to the target machine.

If you are trying to connect to a home computer behind an ADSL modem with a static IP address, your ADSL modem needs to be configured with port forwarding to connect the external port 1433 with the same port on the internal address. This must be done on the modem, and cannot be done just with a connection string.

Let's say you have an ADSL modem listening on 127.2.3.4 (invalid address for demonstration only) and a PC behind that with an IP address of 192.168.0.100. Configure the modem to forward port 1433 to 192.168.0.100:1433 (how will depend on make and model of modem). Then your connection string would be:

Data Source=127.2.3.4\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password

Assuming that SQLEXPRESS is the only database instance on the server, and since port 1433 is the default, you could use the simpler:

Data Source=tcp:127.2.3.4;Initial Catalog=dbase;User ID=sa;Password=password

The Network Library=DBMSSOCN specification is replaced with tcp: and defaults are assumed for instance and port.

like image 150
Corey Avatar answered Oct 14 '22 12:10

Corey


Solution : if you are providing remote machine IP address then you don't need to provide hostname

Try This:

SqlConnection cs = new SqlConnection(@"Data Source=(IP Address)\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password");
like image 31
Sudhakar Tillapudi Avatar answered Oct 14 '22 12:10

Sudhakar Tillapudi


Why don't you try SqlConnection String Builder here.

like image 35
vkstarry Avatar answered Oct 14 '22 11:10

vkstarry