select-in-table.aspx
来自「Professional ASP.NET source code」· ASPX 代码 · 共 116 行
ASPX
116 行
<%@Page Language="C#" debug="true"%>
<%@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>Sorting and Filtering in a DataTable Object</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Sorting and Filtering in a DataTable Object</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>SELECT command: <b><span id="outSelect" runat="server"></span></b></div>
<div id="outError" runat="server"> </div>
<div id="outMessage" runat="server"></div>
<form runat="server">
Sort records: <input type="submit" id="cmdTitle" value="By Title" runat="server" />
<input type="submit" id="cmdISBN" value="By ISBN" runat="server" />
<input type="submit" id="cmdDate" value="By Date" runat="server" /><p />
Search within titles: <input type="text" id="txtFind" size="20" runat="server" />
<input type="submit" id="cmdFind" value="Find" runat="server" />
</form>
<div id="outResult" 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;
outConnect.InnerText = strConnect; // and display it
// specify the SELECT statement to extract the data
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '1861003%'";
outSelect.InnerText = strSelect; // 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 DataAdapter using the connection object and select statement
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSelect, objConnect);
// fill the dataset with data from the DataAdapter object
objDataAdapter.Fill(objDataSet, "Books");
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source;
return; // and stop execution
}
// create the Sorting expression
string strSortString = "";
if (Request.Form["cmdTitle"] != null && Request.Form["cmdTitle"].Length > 0)
strSortString = "Title";
if (Request.Form["cmdIsbn"] != null && Request.Form["cmdISBN"].Length > 0)
strSortString = "ISBN";
if (Request.Form["cmdDate"] != null && Request.Form["cmdDate"].Length > 0)
strSortString = "PublicationDate DESC, Title";
// or create the Filter expression
string strFilterExpr = "";
if (Request.Form["cmdFind"] != null && Request.Form["cmdFind"].Length > 0)
strFilterExpr = "Title LIKE '*" + txtFind.Value + "*'";
// display the parameters we're using
outMessage.InnerHtml = "Called <b>DataTable.Select</b>('<b>"
+ strFilterExpr + "</b>', '<b>" + strSortString + "</b>')";
// get a reference to the "Books" DataTable object
DataTable objTable = objDataSet.Tables["Books"];
// create an array to hold the results then call the Select method
DataRow[] objResults = objTable.Select(strFilterExpr, strSortString);
// the result is an array of DataRow objects not a DataTable object
// so we have to iterate through to get the row contents
string strResult = "<table>";
foreach (DataRow objRow in objResults)
{
strResult += "<tr><td>" + objRow[0] + "</td><td> " + objRow[1]
+ "</td><td> " + objRow[2] + "</td></tr>";
}
strResult += "</table>";
outResult.InnerHtml = strResult; // and display the results
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?