CSDN博客

img bgu

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

发表于2004/9/21 20:17:00  932人阅读

分类: [My ASP/ASP.net(C#)]

<%@ 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>

0 0

相关博文

我的热门文章

img
取 消
img