使用DataAdapter填充多個表(利用DataRelation)的執行個體代碼_實用技巧

來源:互聯網
上載者:User

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>無標題頁</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);//建立資料庫連接
        string sqlCategory = "select ID,C_Name from Photo_Category";//查詢相簿分類表中資訊
        string sqlPhoto = "select CategoryID,Title from Photo";//查詢相簿表中資訊
        SqlDataAdapter da = new SqlDataAdapter(sqlCategory, mycon);//建立資料配接器
        DataSet ds = new DataSet();//建立資料集
        try
        {
            if (mycon.State.Equals(ConnectionState.Closed))
            { mycon.Open(); }//顯式地開啟資料庫連接
            da.Fill(ds, "Photo_Category");//填充相簿分類表
            da.SelectCommand.CommandText = sqlPhoto;
            da.Fill(ds, "Photo");//填充相簿資訊表
        }
        finally
        {
            mycon.Close();//顯式地關閉資料庫連接
        }
        //建立DataRelation對象,關聯表間關係
        DataRelation relat = new DataRelation("Photo_Category",  ds.Tables["Photo_Category"].Columns["ID"],ds.Tables["Photo"].Columns["CategoryID"]);
        ds.Relations.Add(relat);//添加表間關係
        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();//將運行結果輸出到頁面中
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.