. NET MVC Connection data Local database three ways

Source: Internet
Author: User
Tags connectionstrings

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

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.