简体   繁体   中英

Error while connecting to remote SQL Server database

My problem is quite simple: I'm writing a WinForms app in .net 4.0 C# in Visual Studio 2010 it works nicely on my computer.

It connects to a remote SQL Server database using System.Data.SqlClient 's SqlConnection .

When the program loads the first form it runs the following code:

SqlConnection ACconnection = new SqlConnection(String.Format(
        "Server=xxx.xx.xxx.xxx\\TEST;Database=REP01;User Id={0};Password={1};", User, Password));

private bool TestConnection()
{
    try
    {
        ACconnection.Open();
        lbl_connectionStatus.Text = "Server: Connected";
        lbl_connectionStatus.ForeColor = Color.FromArgb(0, 150, 30);
        ACconnection.Close();
        return true;
    }
    catch (Exception ex)
    {
        Functions.GetError(ex);
        return false;
    }            
}

I get the error:

SQL Network Interfaces, error: 26

My problem is that even though it is working fine on my computer it won't work nearly anywhere else. All the computers I've tried it are running on Windows 7 or 8, including mine.

I've googled the error, but all the answers focus on the server side, and since it's working fine from my computer I know it's not a server side problem.

Assuming you have firewall issues addressed (either turn off the firewall on your SQL Server box, or make sure there's a hole at port 1433, or whatever port your SQL Server instance is configured to use), make sure you enable remote connections:

From SQL Server Management Studio on the box with the SQL Server instance running, connect to your instance, right-click the server instance in the left pane, and select "Properties". Click the "Connections" tab, and ensure the "Allow remote connections to this server" is checked.

And finally launch your "SQL Server Configuration Manager" tool, and ensure you have Shared Memory, TCP/IP, and Named Pipes all enabled for client protocols for your client configuration. You'll need to do this for both 32 bit and 64 bit.

You should be good to go then.

I figured it out! The problem was that even though I gave only an instance name in the connection string and no port. So eventually my code was off. The solution: give the instance name and the port also.

SqlConnection ACconnection = new SqlConnection(String.Format(
    "Server=xxx.xx.xxx.xxx\\TEST,49232;Database=REP01;User Id={0};Password={1};", User, Password));

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