1
votes

A developer friend and I could not get a connection string to work on my PC. I'm calling to a local db instance which is Management Studio 2014 (one of the free editions).
It fails on connection.Open();

The Code is:

string conn = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ToString();
            IDbConnection connection = new SqlConnection(conn);
            connection.Open();

App.Config is:
  <connectionStrings>
    <add name="DatabaseConnection"
         connectionString="data source=MIKE-PC; initial catalog=dbname;integrated security =True"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

The Error is:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information:
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

1
did you do that: "Verify that the instance name is correct and that SQL Server is configured to allow remote connections."? - vmg

1 Answers

0
votes

There are many possible issues that you can check to correct this:

  1. Make sure your database server is running: open run or shortcut Window + R, type services.msc, hit enter. Make sure the SQL Server (SQLEXPRESS) service is running.
  2. Open your SQL Management Studio. Try to connect to your database from there to make sure your server name (data source) and database name (initial catalog) are correct.
  3. Make sure your database can be accessed using Integrated Security. You can tell that it does if you can connect to your database using "Windows Authentication" from your MS SQL Server Management Studio.

Hope this help.