asp.net空网站
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//未使用异常处理,页面直接看到错误
string strConn = @"Data Source=\SQLEXPRESS;Initial Catalog = 'D:\xxx.MDF;'Integrated Security= True";
System.Data.SqlClient.SqlConnection sql1 = new System.Data.SqlClient.SqlConnection(strConn);
sql1.Open();
}
}
使用异常处理后
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//使用异常处理后
string strConn = @"Data Source=\SQLEXPRESS;Initial Catalog='D:\xxx.MDF';Integrated Security=True";
System.Data.SqlClient.SqlConnection sql2 = new System.Data.SqlClient.SqlConnection(strConn);
try
{
sql2.Open();
}
catch (Exception ex)
{
Response.Write("系统提示:" + ex.Message + "<br>");
Response.Write("用户自定义:打开数据库错误!" + "<br>");
}
finally
{
sql2.Close();
Response.Write("程序执行至finally了。");
}
}
}