ASP. NET connection Database Test (VS2010)

Source: Internet
Author: User
Tags connectionstrings

1. Create a new ASP. NET site template; Double-click the Web. config file to add a <connectionStrings> in the <configuration> and </configuration> nodes Node with the following code:

<connectionStrings>    <add name= "Con"         connectionstring= "Server=yayun\sqlexpress;database=hotel; User Id=sa; Password=1111qq "/>  </connectionStrings>

Sever is the name of the server; database is the name; user ID is the login username; password is the login password.

After adding the whole code is as follows:

<?xml version= "1.0"?><!--For more information about how to configure an ASP. NET application, go to http://go.microsoft.com/fwlink/? linkid=169433--><configuration> <connectionStrings> <add name= "Con" connectionstring= "serv Er=yayun\sqlexpress;database=hotel; User Id=sa; Password=1111qq "/> </connectionStrings> <!--<connectionStrings> <add name=" ApplicationServices "connectionstring=" Data source=.\sqlexpress;integrated Security=sspi; attachdbfilename=| Datadirectory|\aspnetdb.mdf; User instance=true "providername=" System.Data.SqlClient "/> </connectionStrings>--> &LT;SYSTEM.WEB&G    T <compilation debug= "True" targetframework= "4.0"/> <authentication mode= "Forms" > <forms loginurl= "~ /account/login.aspx "timeout=" 2880 "/> </authentication> <membership> <providers> & lt;clear/> <add name= "AspNetSqlMembershipProvider" type= "System.Web.Security.SqlMembershipProvider" connEctionstringname= "ApplicationServices" enablepasswordretrieval= "false" enablepasswordreset= "true" requiresQues  Tionandanswer= "false" Requiresuniqueemail= "false" maxinvalidpasswordattempts= "5" minrequiredpasswordlength= "6" minrequirednonalphanumericcharacters= "0" passwordattemptwindow= "applicationname="/"/> </provi ders> </membership> <profile> <providers> <clear/> <add name= "Aspne Tsqlprofileprovider "type=" System.Web.Profile.SqlProfileProvider "connectionstringname=" ApplicationServices " Applicationname= "/"/> </providers> </profile> <rolemanager enabled= "false" > <provi ders> <clear/> <add name= "AspNetSqlRoleProvider" type= "System.Web.Security.SqlRoleProvider" conn Ectionstringname= "ApplicationServices" applicationname= "/"/> <add name= "AspNetWindowsTokenRoleProvider" type = "System.Web.Security.WindowsTokenRolEprovider "applicationname="/"/> </providers> </roleManager> </system.web> <system.webs Erver> <modules runallmanagedmodulesforallrequests= "true"/> </system.webserver></configuration >
2. Double-click the Default.aspx file to drag a Label control from the Toolbox into Design view.

Double-click Default.aspx.cs under Solution Explorer to write the following code:

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using system.web.ui.webcontrols;using system.data.sqlclient;namespace webapplication7{Public    partial class _Default: System.Web.UI.Page    {        protected void Page_Load (object sender, EventArgs e)        {            SqlConnection Connecton = New SqlConnection (system.web.configuration.webconfigurationmanager.connectionstrings["Con"]. Connectionstring.tostring ());            Try            {                Connecton. Open ();                This. Label1.Text = "Connection database Test succeeded!" ";            }            catch (Exception Err)            {this                . Label1.Text = "Failed to connect to database!" ";                Label1.Text + = err;            }            Finally             {                Connecton. Close ();}}}    
The debug results are as follows:

Connection is not successful the general problem is the code in the Web. config file, i.e.

<connectionStrings>    <add name= "Con"         connectionstring= "Server=yayun\sqlexpress;database=hotel; User Id=sa; Password=1111qq "/>  </connectionStrings>
Note case, space, character input, and the correctness of the respective names!

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.