0

I am trying to connect to a SQL Server 2005 Express edition remotely using c#, but it is throwing an exception.

A network related or instance specific error occured while establishing a connection to SQL Server .The server was not found or was not accessible.Verify that the instance name is correct and the SQL Server is configured to allow remote connection. (provider :Name Pipes Provider,error:40 - Could not open a connection to SQL Server)

Below is the code I am using to connect to the database

    private void button_test_Click(object sender, EventArgs e)
    {
        try
        {
            string str = "data source=" + textBox_server.Text + "; initial catalog=" + textBox_db.Text
    + "; user id=" + textBox_user.Text + "; pwd=" + textBox_password.Text + ";";

            SqlConnection sqlcon = new SqlConnection(str);
            sqlcon.Open();
            sqlcon.Close();
            MessageBox.Show("Test Connection was successfull");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Test Connection failed. "+ ex.Message);
        }

I am entering the correct ip, database table name, username and password.

Verified everything in the SQL Server Express configurations. Everything is fine.

So where am I going wrong?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sohail
  • 780
  • 3
  • 14
  • 27
  • 1
    Is the sql server on a standalone PC? If so, you may need to open a specific port. – Derek Dec 03 '12 at 14:34
  • You don't mention a firewall, you will need to configure windows firewall to allow connections (On the PC where SQL Express is running) – hollystyles Dec 03 '12 at 14:38
  • ya i have configure the windows firewall for both pcs – Sohail Dec 03 '12 at 14:43
  • @Derek i am connecting to different pc , connected via two cross cable and set the ips for both. we are able to ping. Everything is configured – Sohail Dec 03 '12 at 14:45
  • Try to force usage of TCP provider by adding following parameter to connection string Network=DBMSSOCN; – Oleg Dec 03 '12 at 14:46

2 Answers2

2

This is normally one out of 2 problems. It could be a firewall blocking, easiest way to test that is to try and telnet to the SQL server port (default is 1433).

The other problem that often occurs is that the correct protocol is not enabled on the SQL server express. Here is a good description of how to do it. http://www.teratrax.com/connecting-sql-server-express/

devzero
  • 2,510
  • 4
  • 35
  • 55
0

On the client machine, try by deactivating the firewall.

On the server machine, deactivate the firewall BUT also check that you activated the TCP/IP protocol for the desired instance of MSSQL:

  • Application "Sql Server Configuration Manager"
  • Expand the node "SQL Server Network Configuration"
  • Click on "Protocols on XXXXXXX" (select the desired SQL instance)
  • Right/Click on TCP/IP and select "enable"

If you want to change the port to listen; simply edit the properties of "TCP/IP"

Serge

Serge Bollaerts
  • 324
  • 2
  • 6