简体   繁体   中英

Not able to connect SQL Server 2005 Express using c# remotely

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?

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM