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=190.190.200.100,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=192.168.15.86,1433;Network Library=DBMSSOCN;Initial Catalog=test1;User ID=sa;Password=sa;");
mcon.Open();
SqlCommand mcmd = new SqlCommand("insert into table1 values('test1234')", mcon);
mcmd.ExecuteNonQuery();


NB:

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

when connecting through the MSDASQL provider use the syntax :
"Network=DBMSSOCN".

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails