nested-data-access.aspx

来自「东软内部材料(四)asp等相关的教学案例 」· ASPX 代码 · 共 90 行

ASPX
90
字号
<%@Page Language="C#"%>

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>

<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<%@ Register TagPrefix="wrox" TagName="getdataset" Src="..\global\get-dataset-control.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Displaying Nested Relational Data</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Displaying Nested Relational Data</span><hr />
<!--------------------------------------------------------------------------->

<%-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/>

<%-- insert the control that creates the DataSet --%>
<wrox:getdataset id="ctlDataSet" runat="server"/>

<div id="divResults" runat="server"></div>

<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;

		// create a variable to hold an instance of a DataSet object
		DataSet objDataSet;

		// get dataset from get-dataset-control.ascx user control
		objDataSet = ctlDataSet.BooksDataSet(strConnect, "ISBN LIKE '18610014%'");

		if (objDataSet == null)
			return;

		// now we're ready to display the contents of the DataSet object
		// create a string to hold the results
		string strResult = "";

		// create a reference to our main Books table in the DataSet
		DataTable objTable = objDataSet.Tables["Books"];

		// create references to each of the relationship objects in the DataSet
		DataRelation objAuthorRelation = objTable.ChildRelations["BookAuthors"];
		DataRelation objPriceRelation = objTable.ChildRelations["BookPrices"];

		// now we can iterate through the rows in the Books table
		foreach (DataRow objRow in objTable.Rows)
		{
			// get the book details and append them to the "results" string
			strResult += "<b>" + objRow["Title"] + "</b><br />&nbsp; ISBN: " + objRow["ISBN"]
						+ " &nbsp; Release Date: " + objRow["PublicationDate"] + "<br />";

			// get a collection (array) of all the matching Author table rows for this row
			DataRow[] colChildRows = objRow.GetChildRows(objAuthorRelation);

			strResult += "&nbsp; Author(s): ";

			// iterate through all the matching Author records adding to the result string
			foreach (DataRow objChildRow in colChildRows)
				strResult += objChildRow["FirstName"] + " " + objChildRow["LastName"] + ", ";
			strResult += "<br />";

			// repeat using the Price table relationship to display data from matching Price records
			colChildRows = objRow.GetChildRows(objPriceRelation);
			strResult += "&nbsp; Price: ";
			foreach (DataRow objChildRow in colChildRows)
				strResult += objChildRow["Currency"] + ":" + objChildRow["Price"] + " &nbsp; ";
			strResult += "<p />";

		}	// and repeat for next row in Books table

		divResults.InnerHtml = strResult;  // display the results

	}
</script>


<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?