The right connection string for Remote SQL server for C#

天涯浪子 提交于 2019-12-02 18:35:53

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.

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");

Why don't you try SqlConnection String Builder here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!