How do I connect to an .mdf (Microsoft SQL Server Database File) in a simple web project?

Specifically, in VS 2008, I want to connect to a data source that you can have by right-clicking on the automatically-generated App_Data folder (an .mdf “database”). Seems easy, and it is once you know how.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

A great resource I always keep around is connectionstrings.com.
It’s really handy for finding these connection strings when you can’t find an example.

Particularly this page applied to your problem

Attach a database file on connect to a local SQL Server Express instance

Driver={SQL Native Client};Server=.SQLExpress;AttachDbFilename=c:asdqwemydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

Method 2

So here’s the answer from MSDN:

Choos[e] “Add New Data Source” from the
Data menu.[And follow the connection wizard]

Very easy, except that I have no Data menu. If you don’t have a Data menu, do the following:

  • Click on Tools >> Connect to Database…
  • Select “Microsoft SQL Server Database File”, take the default Data provider, and click OK
  • On the next screen, browse to your Database file, which will be in your VS Solution folder structure somewhere.

Test the connection. It’ll be good. If you want to add the string to the web.config, click the Advanced button, and copy the Data Source line (at the bottom of the dialog box), and paste it into a connection string in the appropriate place in the web.config file. You will have to add the “AttachDbFilename” attribute and value. Example:

The raw text from the Advanced panel:

Data Source=.SQLEXPRESS;Integrated Security=True;Connect Timeout=30;User Instance=True

The actual entry in the web.config:

<add name="SomeDataBase" connectionString="Data Source=.SQLEXPRESS; 
AttachDbFilename=C:DevelopmentblahBlahApp_DataSomeDataFile.mdf;
Integrated Security=True; Connect Timeout=30; User Instance=True" />

Method 3

Just one more — i’ve always kept a udl file on my desktop to easily create and test connection strings. If you’ve never done it before – create a new text file and name it to connection.udl (the ext is the only important part). Open the file, start on the Provider tab and work your way through. Once you’re happy with the connection rename the file giving it a .txt extension. Open the file and copy the string – it’s relatively easy and lets you test the connection before using it.

Method 4

<add name="Your Database" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|Expanse.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/>

Method 5

In your Login.aspx.cs (the code behind file for your login page in the submit button click event) add

string constr = @"Data Source=(LocalDB)v11.0; AttachDbFilename=|DataDirectory|myData.mdf; Integrated Security=True; Connect Timeout=30;";
using (SqlConnection conn = new SqlConnection(constr))
string constr =    ConfigurationManager.ConnectionStrings["myData"].ToString();

using (SqlConnection conn = new SqlConnection(constr))
{
sqlQuery=" Your Query here"
SqlCommand com = new SqlCommand(sqlQuery, conn);
com.Connection.Open();
string strOutput = (string)com.ExecuteScalar();
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x