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

ASP
揭开ASP神秘面纱(2)
揭开ASP神秘面纱(3)
揭开ASP神秘面纱(4)
揭开ASP神秘面纱(5)
让网站活起来(1)
让网站活起来(2)
让网站活起来(3)
让网站活起来(4)
如何在ASP.Net中取得环境变量的值
将HTML表单数据存储为XML格式(1)
将HTML表单数据存储为XML格式(2)
将HTML表单数据存储为XML格式(3)
ASP.Net调试之三板斧:第一招
ASP.Net调试之三板斧:第二招
ASP.Net调试之三板斧:第三招
新闻更新系统(1)
新闻更新系统(2)
新闻更新系统(3)
ubb风格论坛中自您上次来后有新的贴子的功能的asp实现
一个新版本的ubb转化程序

亲密接触ASP.Net(9)


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