使用DataAdapter填充多個表(利用DataRelation)的實(shí)例代碼

Default.aspx
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>無標(biāo)題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="lbText" runat="server"></asp:Label>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationSettings.AppSettings["strCon"];
SqlConnection mycon = new SqlConnection(connectionString);//創(chuàng)建數(shù)據(jù)庫連接
string sqlCategory = "select ID,C_Name from Photo_Category";//查詢相冊分類表中信息
string sqlPhoto = "select CategoryID,Title from Photo";//查詢相冊表中信息
SqlDataAdapter da = new SqlDataAdapter(sqlCategory, mycon);//創(chuàng)建數(shù)據(jù)適配器
DataSet ds = new DataSet();//創(chuàng)建數(shù)據(jù)集
try
{
if (mycon.State.Equals(ConnectionState.Closed))
{ mycon.Open(); }//顯式地打開數(shù)據(jù)庫連接
da.Fill(ds, "Photo_Category");//填充相冊分類表
da.SelectCommand.CommandText = sqlPhoto;
da.Fill(ds, "Photo");//填充相冊信息表
}
finally
{
mycon.Close();//顯式地關(guān)閉數(shù)據(jù)庫連接
}
//創(chuàng)建DataRelation對象,關(guān)聯(lián)表間關(guān)系
DataRelation relat = new DataRelation("Photo_Category", ds.Tables["Photo_Category"].Columns["ID"],ds.Tables["Photo"].Columns["CategoryID"]);
ds.Relations.Add(relat);//添加表間關(guān)系
StringBuilder builder = new StringBuilder("");
foreach (DataRow row in ds.Tables["Photo_Category"].Rows)
{
builder.Append("<b>");
builder.Append(row["C_Name"].ToString());
builder.Append("</b><ul>");
DataRow[] childRows = row.GetChildRows(relat);
foreach (DataRow childRow in childRows)
{
builder.Append("<li>");
builder.Append(childRow["Title"].ToString());
builder.Append("</li>");
}
builder.Append("</ul>");
}
lbText.Text += builder.ToString();//將運(yùn)行結(jié)果輸出到頁面中
}
}
相關(guān)文章
限制CheckBoxList控件只能單選實(shí)現(xiàn)代碼及演示動畫
開發(fā)要求,原本對CheckBoxList控件是用來讓用戶多選的。但現(xiàn)在特殊要求,這個CheckBoxList控件限制只能單選,很多新手朋友可能不知從何下手,為此本文的出現(xiàn)時有必要的了,有需要的朋友可以了解此文2013-01-01
設(shè)置默認(rèn)Ajax操作cache and error
設(shè)置默認(rèn)Ajax操作cache and error,需要的朋友可以參考一下2013-02-02
asp.net實(shí)現(xiàn)微信公眾賬號接口開發(fā)教程
這篇文章主要是一篇關(guān)于asp.net實(shí)現(xiàn)微信公眾賬號接口開發(fā)教程,感興趣的小伙伴們可以參考一下2015-12-12
在.NET Web API設(shè)置響應(yīng)輸出Json數(shù)據(jù)格式常用的兩種方式詳解
在ASP.NET Core Web API中設(shè)置響應(yīng)輸出Json數(shù)據(jù)格式常用以下兩種方式:可以通過添加System.Text.Json或Newtonsoft.JsonJSON序列化和反序列化庫在應(yīng)用程序中全局設(shè)置接口響應(yīng)的Json數(shù)據(jù)格式,本文示例使用的是新的Minimal API模式,感興趣的朋友跟隨小編一起看看吧2024-07-07
C# 獲取當(dāng)前星期幾三種實(shí)現(xiàn)方法
獲取當(dāng)前星期幾實(shí)現(xiàn)這個功能有多種方法,接下來將列出3種供你參考,感興趣的你可不要錯過了哈,希望本文所提供的知識點(diǎn)對你有所幫助2013-02-02

