📄 multiple-dataset-oledb.aspx
字号:
<%@Page Language="C#"%>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>
<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>The DataSet Object with Multiple Tables</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">The DataSet Object with Multiple Tables</span><hr />
<!--------------------------------------------------------------------------->
<%-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/>
<div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>Books SELECT command: <b><span id="outSelectBooks" runat="server"></span></b></div>
<div>Authors SELECT command: <b><span id="outSelectAuthors" runat="server"></span></b></div>
<div>Prices SELECT command: <b><span id="outSelectPrices" runat="server"></span></b></div>
<div id="outError" runat="server">+nbsp;</div>
<b>DataSet.Tables Collection</b>
<asp:datagrid id="dgrTables" runat="server" /><br />
<b>DataSet.Relations Collection</b>
<asp:datagrid id="dgrRelations" runat="server" /><br />
<b>Contents of DataSet.Tables["Books"]</b>
<asp:datagrid id="dgrBooksData" runat="server" /><br />
<b>Contents of DataSet.Tables["Authors"]</b>
<asp:datagrid id="dgrAuthorsData" runat="server" /><br />
<b>Contents of DataSet.Tables["Prices"]</b>
<asp:datagrid id="dgrPricesData" runat="server" />
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// get connection string from ..\global\connect-strings.ascx user control
string strConnect = ctlConnectStrings.OLEDBConnectionString;
outConnect.InnerText = strConnect; // and display it
// specify the SELECT statement to extract the BookList data
string strSelectBooks = "SELECT * FROM BookList WHERE ISBN LIKE '18610033%'";
outSelectBooks.InnerText = strSelectBooks; // and display it
// specify the SELECT statement to extract the BookAuthor data
string strSelectAuthors = "SELECT * FROM BookAuthors WHERE ISBN LIKE '18610033%'";
outSelectAuthors.InnerText = strSelectAuthors; // and display it
// specify the SELECT statement to extract the BookPrices data
string strSelectPrices = "SELECT * FROM BookPrices WHERE ISBN LIKE '18610033%'";
outSelectPrices.InnerText = strSelectPrices ; // and display it
// declare a variable to hold a DataSet object
// note that we have to create it outside the Try..Catch block
// as this is a separate block and so is a different scope
DataSet objDataSet = new DataSet();
try
{
// create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect);
// create a new Command object
OleDbCommand objCommand = new OleDbCommand();
// set the properties
objCommand.Connection = objConnect;
objCommand.CommandType = CommandType.Text;
objCommand.CommandText = strSelectBooks;
// create a new DataAdapter object
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter();
// and assign the Command object to it
objDataAdapter.SelectCommand = objCommand;
// get the data from the "BookList" table in the database and
// put it into a table named "Books" in the DataSet object
objDataAdapter.Fill(objDataSet, "Books");
// change the SELECT statement in the Command object
objCommand.CommandText = strSelectAuthors;
// then get data from "BookAuthors" table into the DataSet
objDataAdapter.Fill(objDataSet, "Authors");
// and do the same again to get the "BookPrices" data
objCommand.CommandText = strSelectPrices;
objDataAdapter.Fill(objDataSet, "Prices");
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accesing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source;
return; // and stop execution
}
// declare a variable to hold a DataRelation object
DataRelation objRelation;
// create a Relation object to link Books and Authors
objRelation = new DataRelation("BookAuthors",
objDataSet.Tables["Books"].Columns["ISBN"],
objDataSet.Tables["Authors"].Columns["ISBN"]);
// and add it to the DataSet object's Relations collection
objDataSet.Relations.Add(objRelation);
// now do the same to link Books and Prices
objRelation = new DataRelation("BookPrices",
objDataSet.Tables["Books"].Columns["ISBN"],
objDataSet.Tables["Prices"].Columns["ISBN"]);
objDataSet.Relations.Add(objRelation);
// now we're ready to display the contents of the DataSet object
// bind the collection of Tables to the first DataGrid on the page
dgrTables.DataSource = objDataSet.Tables;
dgrTables.DataBind();
// bind the collection of Relations to the second DataGrid on the page
dgrRelations.DataSource = objDataSet.Relations;
dgrRelations.DataBind();
// create a DataView object to use with the tables in the DataSet
DataView objDataView = new DataView();
// get the default view of the Books table into the DataView object
objDataView = objDataSet.Tables["Books"].DefaultView;
// and bind it to the third DataGrid on the page
dgrBooksData.DataSource = objDataView;
dgrBooksData.DataBind();
// then do the same for the Authors table
objDataView = objDataSet.Tables["Authors"].DefaultView;
dgrAuthorsData.DataSource = objDataView;
dgrAuthorsData.DataBind();
// and finally do the same for the Prices table
objDataView = objDataSet.Tables["Prices"].DefaultView;
dgrPricesData.DataSource = objDataView;
dgrPricesData.DataBind();
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -