1

I have tried everything but I am having trouble getting a basic asp.net site to connect to a database. I'm using Visual Studio and trying to retrieve data from a local database file into a table on a .cshtml page.

The database I am trying to access is in the App_Data folder.

On the Web.config file I have the following under connection strings:

<add name="connectionName" connectionString="Data Source=|DataDirectory|\database.mdf" providerName="System.Data.SqlClient"/>

On the page itself I have:

@{
var DB = Database.Open("connectionName");
var selectQueryString = "SELECT * FROM Clients ORDER BY lastName";
}

<table>
    <thead>
        <tr>
            <td>client_id</td>
            <td>firstName</td>
            <td>lastName</td>
            <td>address</td>
            <td>city</td>
            <td>state</td>
            <td>postalCode</td>
            <td>phone</td>
        </tr>
    </thead>
    @foreach (var row in DB.Query(selectQueryString))
    {
    <tr>
         <td>@row.client_id</td>
         <td>@row.firstName</td>
         <td>@row.lastName</td>
         <td>@row.address</td>
         <td>@row.city</td>
         <td>@row.state</td>
         <td>@row.postalCode</td>
         <td>@row.phone</td>
    </tr>
    }
</table>

The exception that is being thrown is:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

I am also seeing some text stating that the issues may be:

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)

Sunny
  • 3,185
  • 8
  • 34
  • 66
STL-Lucas
  • 11
  • 1

2 Answers2

0

For a local data source with a mdf file in you App_data directory you need to include the AttachDbFilename attribute as file name and set the data source attribute to the be version of the local db you are running. It should look something like this.

<add name="connectionName" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-myapp-mydb.mdf;Initial Catalog=aspnet-myapp-mydb;Integrated Security=True" providerName="System.Data.SqlClient" />
Padhraic
  • 5,112
  • 4
  • 30
  • 39
  • Thank you so much for your input. It looks like this is heading in the right direction but now I'm seeing another exception. An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: The database 'database' cannot be opened because it is version 782. This server supports version 706 and earlier. A downgrade path is not supported. Since I used Visual Studio to both create the .mdf file and to run the program how do I change which server version is being used? – STL-Lucas Sep 07 '15 at 14:10
0

NM. I figured out I needed to change (LocalDB)\v11.0 to (LocalDB)\MSSQLLocalDB no more version 782 error. Thank you so much for your help! Padhraic that was driving me crazy!

STL-Lucas
  • 11
  • 1