1

I have a Microsoft Access Database (.accdb) full of values that I wish to display in a table on my .cshtml razor page. I need to open the connection and then make the table with the values.

I think that the table would be something like this:

<table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Address</th>
                <th>Address 2</th>
                <th>Field of Work</th>
            </tr>
        </thead>

       <tbody>
            @foreach (var row in _____.Query(____))
            {
                <tr>
                    <td>@row.name</td>
                    <td>@row.address</td>
                    <td>@row.address2</td>
                    <td>@row.field</td>
                </tr>
            }
        </tbody>
  </table>

, but I have not been able to open the connection correctly.

Advice please?

1 Answer 1

3

If you are using Access, you should put the database file in App_Data and specify a connection string in your web.config file:

<connectionStrings>
    <add name="MyConnection" 
      connectionString="Provider="Microsoft.ACE.OleDb.12.0;Data Source=|DataDirectory|MyDatabase.accdb;" 
      providerName="System.Data.OleDb" />
</connectionStrings>

|DataDirectory| is a special short-cut that resolves to the location of the App_Data folder.

When you use the Database helper, you pass in the name of the connection string:

var db = Database.Open("MyConnection");
var data = db.Query("SELECT * FROM MyTable");
...
...
<tbody>
    @foreach (var row in data){
        <tr>
            <td>@row.name</td>
            <td>@row.address</td>
            <td>@row.address2</td>
            <td>@row.field</td>
        </tr>
    }
</tbody>

However, if you are using MVC, the database opening and querying should not happen in the view. You should do that in the controller, or better yet, in a data layer. Alternatively, you can look at ASP.NET Web Pages where the data access code does belong in the view: http://asp.net/web-pages.

Sign up to request clarification or add additional context in comments.

3 Comments

it is giving me a "the name 'database' does not exist in the current context". I assume I am missing something. Also, could you explain why the database opening and querying should happen in another layer as opposed to the view?
I got the name to resolve. All I would like to know is what are the benefits of having it in a different layer? Thanks for all the help!
The whole point of using the MVC framework is to leverage the fact that Views are separate from Controllers and both are separate from the Model, which is where data access takes place. This makes code management and testing much easier. These are fairly advanced features, so if you don't need them, I recommend the Web Pages framework. It's a lot simpler to work with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.