📄 datarelation.aspx
字号:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat=server>
void Page_Load(Object sender , EventArgs e)
{
DataSet dstNorthwind;
SqlConnection conNorthwind;
SqlDataAdapter dadNorthwind;
// Grab the Categories and Products table
dstNorthwind = new DataSet();
conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
dadNorthwind = new SqlDataAdapter( "Select * From Categories", conNorthwind );
conNorthwind.Open();
dadNorthwind.Fill( dstNorthwind, "Categories" );
dadNorthwind.SelectCommand = new SqlCommand( "Select * From Products", conNorthwind );
dadNorthwind.Fill( dstNorthwind, "Products" );
conNorthwind.Close();
// Add Parent/Child Relationship
dstNorthwind.Relations.Add( "Categories_Products", dstNorthwind.Tables["Categories"].Columns["CategoryID"], dstNorthwind.Tables["Products"].Columns["CategoryID"] );
// Display each Category and Child Products
foreach (DataRow drowParent in dstNorthwind.Tables["Categories"].Rows)
{
lblOutput.Text = "<h3>" + drowParent["CategoryName"] + "</h3>";
foreach (DataRow drowChild in drowParent.GetChildRows( "Categories_Products" ))
{
lblOutput.Text = "<li>" + drowChild["ProductName"];
}
}
}
</Script>
<html>
<head><title>DataRelation.aspx</title></head>
<body>
<asp:Label
ID="lblOutput"
Runat="Server" />
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -