1 1<appSettings>2 2<add key="webpages:version"Value="2.0.0.0"/>3 3<add key="webpages:enabled"Value="false"/>4 4<add key="Preserveloginurl"Value="true"/>5 5<add key="clientvalidationenabled"Value="true"/>6 6<add key="unobtrusivejavascriptenabled"Value="true"/>7 7<add key="Con"Value="server=.\sqlexpress; User ID = Sa;password = a123456;database = Xsgl1;max Pool size=512;"/>8 8</appSettings>9 9<connectionStrings>Ten Ten<add name="Consql"connectionstring="server= (local) \SQLExpress; User Id = Sa;password = A123456;database = Xsgl1;max Pool size=512;"/> One One</connectionStrings>
Configuration
1 1 Public classHomecontroller:controller2 2 {3 3 //4 4 //GET:/home/5 5 6 6 Publicactionresult Index ()7 7 {8 8 #regionConnect SQL Function One9 9SqlConnectionStringBuilder one =NewSqlConnectionStringBuilder ();Ten TenOne. DataSource ="(local) \\sqlexpress"; One OneOne. InitialCatalog ="XSGL1"; A AOne. UserID ="SA"; - -One. Password ="a123456"; - -One. Maxpoolsize = +; the theSqlConnection SCT =NewSqlConnection (one. ConnectionString); - - #endregion - - #regionConnect SQL function - - //String conn = configurationmanager.appsettings["Con"]. ToString (); + + //SqlConnection SCT = new SqlConnection (conn); - - #endregion + + #regionConnect SQL function Three A A //String conn = configurationmanager.connectionstrings["Consql"]. ConnectionString; at at //SqlConnection SCT = new SqlConnection (conn); - - #endregion - -SqlCommand SCM =NewSqlCommand (); - -Scm. Connection =SCT; - -Scm.commandtype =CommandType.Text; - -Scm.commandtext ="Select Course name from KC where course number = ' A001 '"; in inSCT. Open (); - -SqlDataReader SDR =SCM. ExecuteReader (); to to if(SDR). Read ()) + + { - -Viewbag.hao = sdr["Course Name"]; the the } * *SDR. Close (); $ $ returnView ();Panax Notoginseng Panax Notoginseng } - - PublicActionResult About () the the { + + returnView (); A A } the the}
Controller
1 1 @{22 " Index " ; 3 3 }44 @ViewBag. Hao55
View. NET MVC Connection data Local database three ways