Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 207
MrBoJangles Avatar asked Oct 06 '08 04:10

MrBoJangles


1 Answers

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();
}
like image 141
Sudheesh Avatar answered Oct 18 '22 19:10

Sudheesh