ASP.NET 连接数据库测试(VS2010)

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>
注意大小写,空格,字符输入,以及各名称对应的正确性!

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值