Friday, February 6, 2009

Connecting to a remote SQL Server database in .net

The following demonstrates how to connect to remote database in .net using ipaddress and port number to a remote SQL Server database ;

First of add the following connection string :

"Data Source=,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword ";

When using Network Library=DBMSSOCN we are make using TCP/IP instead of Named Pipes. It has to be noted that you also have to specify the port number on which the particular sql server listens. "1433" is the default port for SQL Server.

Here is a simple example that inserts the value "test1234" into a table named table1 :

SqlConnection mcon = new SqlConnection("Data Source=,1433;Network Library=DBMSSOCN;Initial Catalog=test1;User ID=sa;Password=sa;");
SqlCommand mcmd = new SqlCommand("insert into table1 values('test1234')", mcon);


When connecting through the SQLOLEDB provider use the following syntax:
"Network Library=DBMSSOCN"

when connecting through the MSDASQL provider use the syntax :

No comments:

Post a Comment


Related Posts with Thumbnails