. NET MVC Connection data Local database three ways
<appSettings> <add key= "webpages:version" value= "2.0.0.0"/> <add key= "webpages:enabled" Value= "false"/> <add key= "Preserveloginurl" value= "true"/> <add key= "clientvalidationenabled" Value= "true"/> <add key= "unobtrusivejavascriptenabled" value= "true"/> <add key= "Con" value= " server=.\sqlexpress; User ID = Sa;password = a123456;database = Xsgl1;max Pool size=512; " /> </appSettings> <connectionStrings> <add name= "Consql" connectionstring= "server= (local) \SQLExpress; User Id = Sa;password = A123456;database = Xsgl1;max Pool size=512; " /> </connectionStrings>
Configuration
public class Homecontroller:controller {////GET:/home/public actionresult Index () {#re Gion Connect SQL function one SqlConnectionStringBuilder one = new SqlConnectionStringBuilder (); One. DataSource = "(local) \\sqlexpress"; One. InitialCatalog = "XSGL1"; One. UserID = "sa"; One. Password = "a123456"; One. Maxpoolsize = 512; SqlConnection SCT = new SqlConnection (one. ConnectionString); #endregion #region Connect SQL function//string conn = configurationmanager.appsettings["Con"]. ToString (); SqlConnection SCT = new SqlConnection (conn); #endregion #region Connect SQL function three//string conn = configurationmanager.connectionstrings["Consql"] . ConnectionString; SqlConnection SCT = new SqlConnection (conn); #endregion SqlCommand SCM = new SqlCommand (); Scm. Connection = SCT; Scm.commandtype = CommandType.Text; Scm.commandtext = "Select Course name from KC where course number = ' A001 '; Sct. Open (); SqlDataReader SDR = SCM. ExecuteReader (); if (SDR). Read ()) {Viewbag.hao = sdr["course name"]; } SDR. Close (); return View (); } public ActionResult about () {return View (); } }
Controller
@{ viewbag.title = "Index";} @ViewBag. Hao