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

ASP
ASP编程菜鸟易犯的一个错误
ASP申请单动态添加实现方法及代码
经验总结:ASP与存储过程解析
学以致用 驳“ASP低能论”
使用ASP脚本技术
ASP、Request对象与SQL注入
判断访问是否来自搜索引擎的ASP函数
ASP判断数据库值是否为空的通用函数
大刀阔斧扔掉ASP,充分准备学习ASP.NET
关于创建多级文件夹的ASP函数实例
ASP正则判断取出HTML的图片地址的函数
网页不存在自动给管理员发邮件的ASP代码
ASP教程:等差数列和等比数列通项公式
Recordset对象和Command对象的区别
Filter与updatebatch混合使用实现批量更新
ASP实例:检测整数和长整数的函数
在不支持FSO的服务器上使用XMLDOM创建HTML文件
汉字转拼音ASP函数
网页输出N行N列表格的ASP实现方法
ASP操作数据库的类

亲密接触ASP.Net(9)


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