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

ASP
ASP下经常用的字符串等函数参考资料
asp下连接数据库 ASP链接数据库字符串大全总结
asp内置对象 ObjectContext 事务管理 详解
asp 内置对象 Application 详解
ASP中 SQL语句 使用方法
ASP 中 Split 函数的实例分析
ASP 中 DateDiff 函数详解 主要实现两日期加减操作
asp 存贮过程 (SQL版asp调用存储过程)
利用ASP实现在线生成电话图片效果脚本附演示
使用ASP记录在线用户的数量的代码
关于ASP生成伪参数技巧 简洁实用的伪(僞)参数
asp 关键词字符串分割如何实现方法
用ASP编写的加密和解密类
ASP之处理用Javascript动态添加的表单元素数据的代码
asp下最常用的19个基本技巧
一些Asp技巧和实用解决方法
asp实现一个统计当前在线用户的解决方案
asp下的一个很简单的验证码程序
asp又一个分页的代码例子
asp实现防止从外部提交数据的三种方法

亲密接触ASP.Net(9)


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