Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to access data from other machine in network. I am using oledb connection to access data from remote machine. The code i wrote to connect to sql server was:
C#
try
            {
                OleDbConnection con = new OleDbConnection("Provider=SQLOLEDB;Data Source=192.168.2.12\\BTBP;Connect Timeout=20;Initial Catalog=consultant;User ID=sa;PWD=BTBP;Network Library=DBMSSOCN;");
                con.Open();
                MessageBox.Show(con.State.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
But it is giving an exception : [DBNETLIB][ConnectionOpen (Connect()).]Specified SQL server not found.

I am not able to solve this problem. I have activated the remote connection of sql server on remote server.

What i am missing?
Posted
Updated 8-Feb-12 22:15pm
v2
Comments
Herman<T>.Instance 9-Feb-12 4:18am    
is there a firewall blocking port 1433 or 1434 (since you use a named instance BTBP)
Avinash6474 9-Feb-12 5:00am    
How to check that port number?

using System;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Demonstrates how to work with SqlConnection objects
/// </summary>
class SqlConnectionDemo
{
static void Main()
{ SqlConnection conn = new SqlConnection(
"Data Source=AJAY-FBFC0452;Initial Catalog=fraiser;Integrated Security=True");

SqlDataReader rdr = null;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("Insert into detail values('Rajesh','Rathod')", conn);
cmd.ExecuteNonQuery();
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn != null)
{
conn.Close();
}
}
}
}
you have to close the connection.in finally block..
 
Share this answer
 
v2
Comments
Avinash6474 9-Feb-12 4:59am    
After con.Open() method call i am getting exception. Sql server not found. The sql server db is on other machine in network.
I have created a TCP/IP port number for a sql server instance,and bind the port with ip address of remote ppc on wich db is present.
-> Go to Sql server Configuration Manager-> Select protocol from left hand pane-> Select TCP/IP from right pane and go to properties-> select IP address tab-> Go to "IP All" section copy "TCP Dynamic address"-> go to IP1 -> paste the number at Tcp dynamic address-> set the remote machin address at the IP address field.-> click apply-> ok

In connection string:
connStr = "Provider=SQLOLEDB;Data Source=" + Ip address of remote machine,TCP port number+";Connect Timeout=20;Initial Catalog=" + dbName + ";charset=utf8;Network Library=DBMSSOCN;Integrated Security=SSPI"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900