日期:2015-02-06  浏览次数:22876 次

test.aspx.cs文件
 
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
 
public partial class test : System.Web.UI.Page
{
    SqlConnection sql = new SqlConnection(@"Data Source=.;Initial Catalog=数据库用户名;User ID=数据库用户名;password=数据库密码");
 
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            sql.Open();
            if (sql.State == ConnectionState.Open)
                Response.Write("连接成功!");
        }
        catch (SqlException S)
        {
            Response.Write("连接出错!");
        }
        finally
        {
            sql.Close();
            sql.Dispose();
        }
    }
}
 
 
Web.config文件配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appSettings />
 
    <system.web>
        <!--
            设置 compilation debug="true" 可将调试符号
            插入已编译的页面中。
            但由于这会影响性能,因此请仅在开发过程中将此值o
            设置为 true。
        -->
        <httpRuntime requestValidationMode="2.0" />
        
        <compilation debug="true" defaultLanguage="c#" targetFramework="4.0"></compilation>
 
        <!--
          通过 <authentication> 节可以配置
          安全身份验证模式,ASP.NET 
          使用该模式来识别来访用户身份。 
        -->
        <authentication mode="Windows" />
        <!--
           如果在执行请求的过程中出现未处理的错误,
           则通过 <customErrors> 节
           可以配置相应的处理步骤。具体而言,
           开发人员通过该节可配置要显示的 html 错误页,
           以代替错误堆栈跟踪。
-->
 
 
     
    </system.web>
 
 
</configuration>