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>--> <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!