Summary of three methods for connecting. net MVC to the local database,. netmvc

Source: Internet
Author: User

Summary of three methods for connecting. net MVC to the local database,. netmvc

. Net MVC connection data local database three methods

  <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 () {# region 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 two // 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 No. = '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.