亲密接触ASP.Net(9)

来源:互联网 发布:五迷三道 知乎 编辑:程序博客网 时间:2024/05/22 01:29
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
document.write(baiduCproIFrame());-->我们继续前面的内容,呵呵,这次主要是讲DataSet的强大功能啦:)。上次吹了大家的泡泡(说话不算数的意思,湖南人都能听懂吧:)),这次一定不能再食言了:)

在一个DataSet中储存多个数据表

我们在ASP中很多人习惯于使用RecordSet对象来操作数据库,但是RecordSet有一个的缺点就是一个RecordSet只能储存一个数据表,当我们需要操作多个表时,不得不在多个RecordSet中来回操作,虽然这些在使用习惯后也没有什么,但是对一个新手来说,这也是一个很麻烦人的事情。光是那些变量名就可以搞浑你,现在好了,在ASP.Net中,只需要一个DataSet就可以搞定一切。大大的方便了我们的程序。我们还是老样子,先看一段程序,再来细细讲解。

<%@PageLanguage="C#"%>
<%@ImportNamespace="System.Data"%>
<%@ImportNamespace="System.Data.ADO"%>
<ScriptLanguage="C#"Runat="Server">
publicvoidPage_Load(Objectsrc,EventArgse)
{
    //联结语句
    stringMyConnString="Driver={MicrosoftAccessDriver(*.mdb)};DBQ=c:/test/test.mdb;";
    stringstrComm1="select*fromUserList";
    stringstrComm2="select*fromBookList";

    //打开一个联结
    ADOConnectionMyConnection=newADOConnection(MyConnString);

    //打开两个DataSetCommand
    ADODataSetCommandMyComm1=newADODataSetCommand(strComm1,MyConnection);
    ADODataSetCommandMyComm2=newADODataSetCommand(strComm2,MyConnection);

    DataSetMyDataSet=newDataSet();

    //把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:DataGridid="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:DataGridid="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

我们现在来分析一下代码:

stringMyConnString="Driver={MicrosoftAccessDriver(*.mdb)};DBQ=c:/test/test.mdb;";
stringstrComm1="select*fromUserList";
stringstrComm2="select*fromBookList";

ADOConnectionMyConnection=newADOConnection(MyConnString);

ADODataSetCommandMyComm1=newADODataSetCommand(strComm1,MyConnection);
ADODataSetCommandMyComm2=newADODataSetCommand(strComm2,MyConnection);

这些都只是在作一些准备工作,打开一个联结,并且打开两个DataSetCommand取得两个表的数据。

DataSetMyDataSet=newDataSet();

这是我们程序的关键之地,这里打开了我们要操作的DataSet对象。下面我们就需要将数据表的内容填入DataSet了。

MyComm1.FillDataSet(MyDataSet,"UserList");
MyComm2.FillDataSet(MyDataSet,"BookList");

这里是我们今天的主要内容。前面的几章曾经说过,在一个DataSet中可以包含多种数据,这里我们是往这个名为MyDataSet的DataSet中存放了两个数据库表,其实只要愿意,我们还可以在里面插入XML数据,而且他们是不会出现冲突的。大家可以放心使用,呵呵。

再下面的代码,就是把MyDataSet的数据传送给DataGrid控件显示。这里就不多说了。

这里我们只是简单的说了一下DataSet能存放多个数据表的功能,大家看看好像没有什么特别的。这里看起来确实好像没有什么特别的功能,但是如果配上DataSet中的修改、添加、删除功能,我们操作数据库就变得很简单。我们可以先将数据库读入DataSet,然后在DataSet中修改数据,如果改得觉得不满意,我们还可以使用RejectChanges方法全面恢复,最后一并交给去数据库去更新。

下一节,我们再来讲解,如何使用DataSet来添加,修改,删除数据。
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击