0

Okay, so after about an hour of searching through the web, I'm still having issues trying to create a local DB connection in ASP.net.

I need to create a connection to a local database file, and then simply loop through each record in a table. I'm not sure which file extension to use here (.aspx, .vbhtml, .cshtml?)

This is what I currently have from this W3school tutorial,

' Create local connection to DB file
@{
var db = Database.Open("Properties.mdf"); 
var query = db.Query("SELECT * FROM Properties"); 
}

    <div id="pagewrap">

        ' Loop through records
        @foreach(var row in query)
        {

        <p>@row.StreetAddress</p> 
        <p>@row.City</p> 
        <p>@row.State</p> 

        }

    </div>

I'm currently getting a Connection string "Properties.mdf" was not found. error when running the above code. Any help is greatly appreciated.

1
  • Is Properties.mdf in your app_data folder? Commented May 12, 2014 at 16:45

1 Answer 1

1

That is because "Properties.mdf" is not a connection string. The mdf file cannot be opened just like this on its own - it needs a proper sql server database service. Exemplary connection string could look like this

var db = Database.Open("Server=.\SQLExpress;AttachDbFilename=Properties.mdf;Database=mydatabase;Trusted_Connection=Yes");

Although you need to have sql server installed (in this case your sql server instance would be called SQLExpress). and the connection string highly depends on what version of SQL Server you want to connect to.

Please refer to MSDN article.

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

Comments

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.