码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2014-07-09 09:39:53      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:blog   http   文件   数据   os   2014   

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();
            }

        }
    }
}
调试结果如下:
bubuko.com,布布扣

连接不成功一般问题出在web.config文件的代码,即

<connectionStrings>
    <add name="Con"
         connectionString="server=YAYUN\SQLEXPRESS;DataBase=Hotel;User ID=sa;Password=1111qq" />
  </connectionStrings>
注意大小写,空格,字符输入,以及各名称对应的正确性!

ASP.NET 连接数据库测试(VS2010),布布扣,bubuko.com

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

标签:blog   http   文件   数据   os   2014   

原文地址:http://blog.csdn.net/yayun0516/article/details/37559003

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!