theChampion
theChampion

Reputation: 4457

Connect To Local Database File (.mdf)

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)

Upvotes: 0

Views: 3858

Answers (1)

Jalpesh Vadgama
Jalpesh Vadgama

Reputation: 14216

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

Upvotes: 1

Related Questions