table-mappings.aspx

来自「Professional ASP.NET source code」· ASPX 代码 · 共 155 行

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

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Using Default and Specific Table and Column Mappings</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Using Default and Specific Table and Column Mappings</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 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(0)</b>
<asp:datagrid id="dgrBooksData" runat="server" /><br />
<b>Contents of DataSet.Tables(1)</b>
<asp:datagrid id="dgrAuthorsData" runat="server" /><br />

<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

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

		// declare a variable to hold a DataTableMapping object
		DataTableMapping objTableMapping;

		// add the default table mapping "Table" - this table name will be used
		// if you don't provide a table name when filling the DataSet
		objTableMapping = objDataAdapter.TableMappings.Add("Table", "DefaultBookList");

		// now add the column mappings for this table
		objTableMapping.ColumnMappings.Add("ISBN", "BookCode");
		objTableMapping.ColumnMappings.Add("Title", "BookTitle");
		objTableMapping.ColumnMappings.Add("PublicationDate", "Published");

		// add a table mapping so that data from the table named "BookAuthors" in
		// the database will be placed in a table named "AuthorList"
		objTableMapping = objDataAdapter.TableMappings.Add("BookAuthors", "AuthorList");

		// add the column mappings for this table
		objTableMapping.ColumnMappings.Add("ISBN", "BookCode");
		objTableMapping.ColumnMappings.Add("FirstName", "Forename");
		objTableMapping.ColumnMappings.Add("LastName", "Surname");

		// new we're ready to fill a DataSet object from the database

		// create a new DataSet object
		DataSet objDataSet = new DataSet();

		try
		{
			// get the data from the "BookList" table in the database and
			// put it into a the default table in the DataSet object
			objDataAdapter.Fill(objDataSet);

			// change the SELECT statement in the Command object
			objCommand.CommandText = strSelectAuthors;
			// then get data from "BookAuthors" table in the database
			// and put it into the mapped table in the DataSet
			objDataAdapter.Fill(objDataSet, "BookAuthors");
		}
		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 the two tables
		// note that it uses the new mapped table and column names
		objRelation = new DataRelation("BookAuthors",
								objDataSet.Tables["DefaultBookList"].Columns["BookCode"],
								objDataSet.Tables["AuthorList"].Columns["BookCode"]);
		// and add it to the DataSet object's Relations collection
		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 default table into the DataView object
		objDataView = objDataSet.Tables["DefaultBookList"].DefaultView;
		// and bind it to the third DataGrid on the page
		dgrBooksData.DataSource = objDataView;
		dgrBooksData.DataBind();

		// then do the same for the mapped BookAuthors table
		objDataView = objDataSet.Tables["AuthorList"].DefaultView;
		dgrAuthorsData.DataSource = objDataView;
		dgrAuthorsData.DataBind();

	}
</script>


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

⌨️ 快捷键说明

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