使用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)文章
ASP.NET MVC小結(jié)之基礎(chǔ)篇(二)
本文續(xù)上篇文章,還是介紹些asp.net mvc相關(guān)的基礎(chǔ)知識,非常的詳細(xì),新手朋友們看看,高手們略過吧2014-11-11Asp.Net Core對接釘釘群機(jī)器人的完整步驟記錄
這篇文章主要給大家介紹了關(guān)于Asp.Net Core對接釘釘群機(jī)器人的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03ASP.NET Core中如何使用表達(dá)式樹創(chuàng)建URL詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中如何使用表達(dá)式樹創(chuàng)建URL的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10ASP.NET Core對Controller進(jìn)行單元測試的完整步驟
這篇文章主要給大家介紹了關(guān)于ASP.NET Core對Controller進(jìn)行單元測試的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06