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

ASP
ASP用户登录模块的设计
ASP:一个网站空间多个域名访问
ASP教程:自己写的数据库操作类
ASP处理多关键词查询实例代码
ASP实例:幻灯片新闻代码
ASP实例:处理多关键词查询实例代码
快速掌握ASP+Access数据库的18条安全法则
快速掌握ASP连接11种数据库的常用语法
在ASP应用中如何限制同一表单被多次提交
ASP防止同时登陆的问题
关于Asp.net ajax下的异常处理
ASP生成html的新方法
ASP中的面向对象 类
ASP调用带参数存储过程的几种方式
asp实现批量录入数据的实现
用ASP调用SQL Server视图和存储过程
值得收藏的一些ASP代码
ASP 编程中 20 个非常有用的例子
每个ASP程序员必备的知识
flash结合asp制作出的显ip,版本,登陆时间,访问次数

亲密接触ASP.Net(9)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 150 ::
收藏到网摘: 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