[我的ASP.net学习历程]Try错误信息处理

类别:Asp 点击:0 评论:0 推荐:

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<script language="C#" runat="server">

 void Page_Load()
 {
  string DB_path = @"Data\db.mdb"; //把路径更改可看到错误信息
  string DB = Server.MapPath(DB_path);
  string strSQL = "SELECT * FROM admin";
  string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DB;
  
  System.Data.OleDb.OleDbDataReader dr;
  System.Data.OleDb.OleDbConnection conn = null;
  System.Data.OleDb.OleDbCommand cmd = null;
  
//=======================================================
  try
  {
   conn = new System.Data.OleDb.OleDbConnection(connString);
   cmd = new System.Data.OleDb.OleDbCommand(strSQL,conn);
   cmd.Connection.Open();
  }
  catch (Exception ex)
  {
   conn.Close();
   Response.Write("Connection was closed!<br>" + ex.Message);
   return;
  }
//=======================================================
  
  dr = cmd.ExecuteReader();
  DataGrid1.DataSource = dr;
  DataGrid1.DataBind();
  conn.Close();

 }
 
</script>

<asp:datagrid ID="DataGrid1" runat="server"></asp:datagrid>

本文地址:http://com.8s8s.com/it/it8586.htm