Three ways to summarize the. NET MVC Connection data Local database

Source: Internet
Author: User
Tags connectionstrings
. 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 
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.