[我的ASP.net学习历程]数据库操作(一)

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

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

 void Page_Load()
 {
   string DB_name = "dbTest.mdb";
   string DB_path = Server.MapPath(DB_name);
   string SQL = "SELECT [CategoryID],[CategoryName] FROM [Categories]";
   string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=" + DB_path ;
   //建立OleDbConnection
   System.Data.IDbConnection dbConnection  = new System.Data.OleDb.OleDbConnection(connectionString);
   
   //建立OleDbCommand
   System.Data.IDbCommand dbCommand = new System.Data.OleDb.OleDbCommand();
   dbCommand.CommandText = SQL;
   dbCommand.Connection = dbConnection;
   
   //建立DataSet
   System.Data.DataSet dbSet = new System.Data.DataSet();
   
   //建立OleDbDataAdapter
   System.Data.IDbDataAdapter dbAdapter = new System.Data.OleDb.OleDbDataAdapter();
   dbAdapter.SelectCommand = dbCommand; //选择OleDbCommand
   dbAdapter.Fill(dbSet); //填充DataSet

   //DataGrid控件操作
   DataGrid1.DataSource = dbSet; //数据源
   DataGrid1.DataBind(); //压入数据
 }
</script>

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

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