strattonn
strattonn

Reputation: 2012

SQL Connection with public IP, named instance and port number

I have had difficulty creating a connection string in c# that will connect to a remote SQL server using a public IP, named instance and a port number (other than 1433). Anyone know how to do that?

Upvotes: 12

Views: 69609

Answers (5)

TPG
TPG

Reputation: 3219

Something like the below, add the port number after the IP or domain name with comma.

<add name="LocalSqlServer" connectionString="Data Source=www.yourdomain.com,1433;Initial Catalog=mydatabase;Persist Security Info=True;User ID=admin;Password=yourstringpassword" providerName="System.Data.SqlClient" />

Upvotes: 0

BlackICE
BlackICE

Reputation: 8926

connectionString="Database=pub;Server=192.168.1.1\INSTANCE,1746;Trusted_Connection=yes;"

Or you could use username/password instead of trusted connection.

Upvotes: 0

ps.
ps.

Reputation: 4360

This site has never failed me.

And i am gonna state the obvious here, but it is generally a bad idea to expose your sql server on the internet.. (unless you are using VPN)

Upvotes: 1

Remus Rusanu
Remus Rusanu

Reputation: 294427

Using the servername tcp:<public IP>,<port>, as documented in SqlConnection.ConnectionString:

The name or network address of the instance of SQL Server to which to connect. The port number can be specified after the server name:

server=tcp:servername, portnumber

When specifying a local instance, always use (local). To force a protocol, add one of the following prefixes:

np:(local), tcp:(local), lpc:(local)

Data Source must use the TCP format or the Named Pipes format.

TCP format is as follows:

  • tcp:<host name>\<instance name>
  • tcp:<host name>,<TCP/IP port number>

If you use the tcp:<host name>\<isntance name> the SQL Browser service connection is required (port 1433) therefore is better to use the later format, with explicit port name:

Data Source=tcp:1.2.3.4,1234;User Id=...; Password=...

Upvotes: 7

D&#39;Arcy Rittich
D&#39;Arcy Rittich

Reputation: 171559

Try this, replacing 666 with the port number you want to use, 190.190.200.100 with the IP address you want, etc.:

Data Source=190.190.200.100\MyInstance,666;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

Upvotes: 26

Related Questions