0

I am trying to connect to local database file (.mdf) which is located in App_Data folder from asp.net dev project. But it gives me an exception. What I have missed ?

Web.Config:

<connectionStrings >
    <add
      name="TestLocalDB"
      connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|DB.mdf;Integrated Security=True;Connect Timeout=30"
      providerName="System.Data.SqlClient"
   />
  </connectionStrings>

code-behind:

string con = ConfigurationManager.ConnectionStrings["TestLocalDB"].ConnectionString;
            SqlConnection connection = new SqlConnection(con);
            connection.Open(); //here it gives the exception

exception:

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)

theChampion
  • 4,207
  • 7
  • 28
  • 35
  • It may cause a lot of possibilities see this article maybe it helps http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/ – Mohamed Farrag Mar 10 '14 at 11:53
  • check this link: http://stackoverflow.com/a/18060818/2746355 hope this solves your issues – Karthik_SD Mar 10 '14 at 12:28

1 Answers1

1

Check whether you have default instance of SQLExpress is installed or not. Otherwise you need to have your instance name of SQL Server.

For more information see following link.

http://technet.microsoft.com/en-us/library/ms143744(v=sql.90).aspx

Jalpesh Vadgama
  • 13,653
  • 19
  • 72
  • 94