当前位置: 首页 > 图文教程 > 网络编程 > ASP > 亲密接触ASP.Net(9)

ASP
FrontPage 2000+DB2实现数据库信息发布
Execel文件插入到ASP页面
利用ASP在客户端注册DLL文件
1栏分页显示(附显示的形式前页,后页)
1栏分页显示(附显示的形式[1][2])
一个基于web的QQ程序 2(xml+asp)
XMLHTTP+javascript+Asp写得聊天室,无刷新实现(一)
XMLHTTP+javascript+Asp写得聊天室,无刷新实现(二)
XMLHTTP+javascript+Asp写得聊天室,无刷新实现(三)
XMLHTTP+javascript+Asp写得聊天室,无刷新实现(四)
XMLHTTP+javascript+Asp写得聊天室,无刷新实现(五)
XMLHTTP+javascript+Asp写得聊天室,无刷新实现(六)
建立一个广告交换及跟踪系统
如何制作无状态的ASP组件
无组件文件上传代码实例
建立动态下拉式选单(三阶层)
用ASP编写的“俄罗斯方块游戏”
利用ASP.NET来访问Excel文档
初探SSI网页制作
具有自攻击性的代码

亲密接触ASP.Net(9)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 99 ::
收藏到网摘: n/a

       我们继续前面的内容,呵呵,这次主要是讲DataSet的强大功能啦:)。上次吹了大家的泡泡(说话不算数的意思,湖南人都能听懂吧:)),这次一定不能再食言了:)
  
  在一个DataSet中储存多个数据表
  
  我们在ASP中很多人习惯于使用RecordSet对象来操作数据库,但是RecordSet有一个的缺点就是一个RecordSet只能储存一个数据表,当我们需要操作多个表时,不得不在多个RecordSet中来回操作,虽然这些在使用习惯后也没有什么,但是对一个新手来说,这也是一个很麻烦人的事情。光是那些变量名就可以搞浑你,现在好了,在ASP.Net中,只需要一个DataSet就可以搞定一切。大大的方便了我们的程序。我们还是老样子,先看一段程序,再来细细讲解。
  
  <% @ Page Language="C#" %>
  <% @ Import Namespace="System.Data" %>
  <% @ Import Namespace="System.Data.ADO" %>
  <Script Language="C#" Runat="Server">
  public void Page_Load(Object src,EventArgs e)
  {
   //联结语句
   string MyConnString = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:/test/test.mdb;";
   string strComm1 = "select * from UserList";
   string strComm2 = "select * from BookList";
  
   //打开一个联结
   ADOConnection MyConnection = new ADOConnection(MyConnString);
  
   //打开两个DataSetCommand
   ADODataSetCommand MyComm1 = new ADODataSetCommand(strComm1,MyConnection);
   ADODataSetCommand MyComm2 = new ADODataSetCommand(strComm2,MyConnection);
  
   DataSet MyDataSet = new DataSet();
  
   //把UserList,BookList表存入DataSet
   MyComm1.FillDataSet(MyDataSet,"UserList");
   MyComm2.FillDataSet(MyDataSet,"BookList");
  
   DataGrid1.DataSource = MyDataSet.Tables["UserList"].DefaultView;
   DataGrid2.DataSource = MyDataSet.Tables["BookList"].DefaultView;
  
   DataGrid1.DataBind();
   DataGrid2.DataBind();
  }
  </script>
  <html>
  <head>
  <title></title>
  </head>
  <body>
  <table>
  <tr>
  <td>
  <ASP:DataGrid id="DataGrid1" runat="server"
  BorderColor="black"
  BorderWidth="1"
  GridLines="Both"
  CellPadding="3"
  CellSpacing="0"
  Font-Name="Verdana"
  Font-Size="8pt"
  HeaderStyle-BackColor="#aaaadd"
  AlternatingItemStyle-BackColor="#eeeeee"
  />
  </td>
  <td>
  <ASP:DataGrid id="DataGrid2" runat="server"
  BorderColor="black"
  BorderWidth="1"
  GridLines="Both"
  CellPadding="3"
  CellSpacing="0"
  Font-Name="Verdana"
  Font-Size="8pt"
  HeaderStyle-BackColor="#aaaadd"
  AlternatingItemStyle-BackColor="#eeeeee"
  />
  </td>
  </tr>
  </table>
  </body>
  </html>
  
  在上面的例子中,我们打开了一个名为test.mdb的Access数据库,然后把他其中的两个表"UserList"和"BookList"使用两个DataGrid控件显示出来。显示的图片如下:
  
  
  图9-1
  
  我们现在来分析一下代码:
  
  string MyConnString = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:/test/test.mdb;";
  string strComm1 = "select * from UserList";
  string strComm2 = "select * from BookList";
  
  ADOConnection MyConnection = new ADOConnection(MyConnString);
  
  ADODataSet