1.新建一个ASP.NET网站模板;双击web.config文件,在<configuration>和</configuration>节点中添加一个<connectionStrings>节点,代码如下:
<connectionStrings>
<add name="Con"
connectionString="server=YAYUN\SQLEXPRESS;DataBase=Hotel;User ID=sa;Password=1111qq" />
</connectionStrings>
sever是服务器名;DataBase是数据库名;User ID是登录用户名;password是登录密码。
加入后整体代码如下:
<?xml version="1.0"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="Con"
connectionString="server=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>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
2.双击Default.aspx文件,从工具箱拖一个Label控件到设计视图中。
双击解决方案资源管理器下的Default.aspx.cs,编写如下代码:
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 = "连接数据库测试成功!";
}
catch (Exception err)
{
this.Label1.Text = "连接数据库失败!";
Label1.Text += err;
}
finally
{
connecton.Close();
}
}
}
}
调试结果如下:
连接不成功一般问题出在web.config文件的代码,即
<connectionStrings>
<add name="Con"
connectionString="server=YAYUN\SQLEXPRESS;DataBase=Hotel;User ID=sa;Password=1111qq" />
</connectionStrings>
注意大小写,空格,字符输入,以及各名称对应的正确性!