0
votes

I'm facing some issues trying to connect to an SQL Server 2005 using Entity Framework.

I've this small WindowsForm application using a System.Data.SqlClient.SqlConnection:

SqlConnection cnn = new SqlConnection("Data Source=servername\\sqlhotel;Database=mydatabase;User Id=myusername;Password=mypassword");

try
{
    cnn.Open();
    MessageBox.Show ("Connection is Open ");
    cnn.Close();
}
catch (Exception ex)
{
    MessageBox.Show("Can not open connection ! ");
}

This is working fine on my server (I'm getting the "Connection is Open" message), but in my ASP.NET Web API application using Entity Framework 6 I'm getting this error using the above connection string:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Why?

1
removing the tags from your question yields: stackoverflow.com/questions/28644482/… - user57508
Please see "Should questions include “tags” in their titles?", where the consensus is "no, they should not"! - user57508

1 Answers

0
votes

Ensure that TCP/IP is enabled in SQL Server Configuration Manager, and that SQL Server is allowed through your Windows Firewall (on the server) if you are trying to connect remotely. You can add it to the white list if not, but for security reasons this is probably a bad idea - and definitely a bad idea if it is a production server.