SQL: CREATE TABLE [ 產品資料 ] ( [ 產品編號 ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL , [ 產品 ] [ nvarchar ] ( 40 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL , [ 供應商編號 ] [ int ] NULL CONSTRAINT [ DF__產品資料__供應商編號__5D60DB10 ] DEFAULT ( 0 ), [ 類別編號 ] [ int ] NULL , [ 單位數量 ] [ nvarchar ] ( 20 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL , [ 單價 ] [ money ] NULL CONSTRAINT [ DF__產品資料__單價__5E54FF49 ] DEFAULT ( 0 ), [ 庫存量 ] [ smallint ] NULL CONSTRAINT [ DF__產品資料__庫存量__5F492382 ] DEFAULT ( 0 ), [ 已訂購量 ] [ smallint ] NULL CONSTRAINT [ DF__產品資料__已訂購量__603D47BB ] DEFAULT ( 0 ), [ 安全存量 ] [ smallint ] NULL CONSTRAINT [ DF__產品資料__安全存量__61316BF4 ] DEFAULT ( 0 ), [ 不再銷售 ] [ bit ] NOT NULL CONSTRAINT [ DF__產品資料__不再銷售__6225902D ] DEFAULT ( 0 ), CONSTRAINT [ Pk_產品資料 ] PRIMARY KEY CLUSTERED ( [ 產品編號 ] ) ON [ PRIMARY ] , CONSTRAINT [ 產品資料_FK00 ] FOREIGN KEY ( [ 類別編號 ] ) REFERENCES [ 產品類別 ] ( [ 類別編號 ] ) ON DELETE CASCADE ON UPDATE CASCADE , CONSTRAINT [ 產品資料_FK01 ] FOREIGN KEY ( [ 供應商編號 ] ) REFERENCES [ 供應商 ] ( [ 供應商編號 ] ) ON DELETE CASCADE ON UPDATE CASCADE , CONSTRAINT [ CK 產品資料 已訂購量 ] CHECK ( [ 已訂購量 ] >= 0 ), CONSTRAINT [ CK 產品資料 安全存量 ] CHECK ( [ 安全存量 ] >= 0 ), CONSTRAINT [ CK 產品資料 庫存量 ] CHECK ( [ 庫存量 ] >= 0 ), CONSTRAINT [ CK 產品資料 單價 ] CHECK ( [ 單價 ] >= 0 )) ON [ PRIMARY ] GO CREATE TABLE [ 產品類別 ] ( [ 類別編號 ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL , [ 類別名稱 ] [ nvarchar ] ( 15 ) COLLATE Chinese_Taiwan_Stroke_CI_AS NOT NULL , [ 說明 ] [ ntext ] COLLATE Chinese_Taiwan_Stroke_CI_AS NULL , [ 圖片 ] [ image ] NULL , [ upsize_ts ] [ timestamp ] NULL , CONSTRAINT [ PK_產品類別 ] PRIMARY KEY CLUSTERED ( [ 類別編號 ] ) ON [ PRIMARY ] ) ON [ PRIMARY ] TEXTIMAGE_ON [ PRIMARY ] GO 运行界面: CS代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace ch1 ... { public partial class BindingDemoForm11 : Form ...{ // 資料集物件的類別層級建立 DataSet ds = new DataSet(); // 關聯性連結物件的類別層級宣告 DataRelation rel; public BindingDemoForm11() ...{ InitializeComponent(); } private void BindingDemoForm11_Load(object sender, System.EventArgs e) ...{ // 設定表單的最小大小 this.MinimumSize = new Size(648, 414); // 建立一個連接字串 string strConnection = "Server=(local);Database=ch1;uid=sa;pwd="; // 建立一個查詢命令字串 string strSql = "SELECT 類別編號, 類別名稱, 說明 FROM 產品類別"; // 建立一個資料連接 SqlConnection myConnection = new SqlConnection(strConnection); // 建立一個資料配接器以便針對資料來源執行 SELECT 陳述式來提取出要填入資料集的資料記錄 SqlDataAdapter myAD = new SqlDataAdapter(strSql, myConnection); // 將資料填入資料集 myAD.Fill(ds, "產品類別"); // 重新指定用來提取資料來源之資料記錄的 SELECT 陳述式 myAD.SelectCommand.CommandText = "SELECT 產品編號, 產品, 供應商編號, 類別編號, 單位數量, 單價, 庫存量, 已訂購量, 安全存量, 不再銷售 FROM 產品資料"; // 將資料填入資料集內名稱為「產品資料」的資料表 myAD.Fill(ds, "產品資料"); // 宣告用來將資料集內之「產品類別」資料表連結至「產品資料」資料表的父欄位與子欄位 DataColumn parentcol; DataColumn childcol; parentcol = ds.Tables["產品類別"].Columns["類別編號"]; childcol = ds.Tables["產品資料"].Columns["類別編號"]; // 建立用來連結「產品類別」資料表與「產品資料」資料表的 DataRelation 物件,其名稱為「每一種類別的產品」。 rel = new DataRelation("每一種類別的產品", parentcol, childcol); ds.Relations.Add(rel); // 設定 ComboBox 控制項的資料來源 ComboBoxCategory.DataSource = ds; // 設定 ComboBox 控制項的顯示欄位 ComboBoxCategory.DisplayMember = "產品類別.類別名稱"; // 將 DataGrid 控制項繫結至 DataRelation 物件(亦即「每一種類別的產品」) DataGridProduct.SetDataBinding(ds, "產品類別.每一種類別的產品"); myConnection.Close(); } }} 窗体: namespace ch1 ... { partial class BindingDemoForm11 ...{ /**//// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /**//// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) ...{ if (disposing && (components != null)) ...{ components.Dispose(); } base.Dispose(disposing); } Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码 /**//// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() ...{ this.Label1 = new System.Windows.Forms.Label(); this.ComboBoxCategory = new System.Windows.Forms.ComboBox(); this.DataGridProduct = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.DataGridProduct)).BeginInit(); this.SuspendLayout(); // // Label1 // this.Label1.AutoSize = true; this.Label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(136))); this.Label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192))))); this.Label1.Location = new System.Drawing.Point(87, 9); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(221, 20); this.Label1.TabIndex = 3; this.Label1.Text = "請選取您所要檢視的產品類別:"; // // ComboBoxCategory // this.ComboBoxCategory.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.ComboBoxCategory.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(136))); this.ComboBoxCategory.ForeColor = System.Drawing.Color.Red; this.ComboBoxCategory.Location = new System.Drawing.Point(371, 7); this.ComboBoxCategory.Name = "ComboBoxCategory"; this.ComboBoxCategory.Size = new System.Drawing.Size(176, 28); this.ComboBoxCategory.TabIndex = 4; // // DataGridProduct // this.DataGridProduct.AlternatingBackColor = System.Drawing.Color.Lavender; this.DataGridProduct.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.DataGridProduct.BackColor = System.Drawing.Color.WhiteSmoke; this.DataGridProduct.BackgroundColor = System.Drawing.Color.LightGray; this.DataGridProduct.CaptionBackColor = System.Drawing.Color.LightSteelBlue; this.DataGridProduct.CaptionForeColor = System.Drawing.Color.MidnightBlue; this.DataGridProduct.CaptionText = "產品基本資料"; this.DataGridProduct.DataMember = ""; this.DataGridProduct.FlatMode = true; this.DataGridProduct.Font = new System.Drawing.Font("Tahoma", 8F); this.DataGridProduct.ForeColor = System.Drawing.Color.MidnightBlue; this.DataGridProduct.GridLineColor = System.Drawing.Color.Gainsboro; this.DataGridProduct.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None; this.DataGridProduct.HeaderBackColor = System.Drawing.Color.MidnightBlue; this.DataGridProduct.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold); this.DataGridProduct.HeaderForeColor = System.Drawing.Color.WhiteSmoke; this.DataGridProduct.LinkColor = System.Drawing.Color.Teal; this.DataGridProduct.Location = new System.Drawing.Point(12, 40); this.DataGridProduct.Name = "DataGridProduct"; this.DataGridProduct.ParentRowsBackColor = System.Drawing.Color.Gainsboro; this.DataGridProduct.ParentRowsForeColor = System.Drawing.Color.MidnightBlue; this.DataGridProduct.SelectionBackColor = System.Drawing.Color.CadetBlue; this.DataGridProduct.SelectionForeColor = System.Drawing.Color.WhiteSmoke; this.DataGridProduct.Size = new System.Drawing.Size(596, 362); this.DataGridProduct.TabIndex = 5; // // BindingDemoForm11 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(612, 414); this.Controls.Add(this.Label1); this.Controls.Add(this.ComboBoxCategory); this.Controls.Add(this.DataGridProduct); this.Name = "BindingDemoForm11"; this.Text = "BindingDemoForm11"; this.Load += new System.EventHandler(this.BindingDemoForm11_Load); ((System.ComponentModel.ISupportInitialize)(this.DataGridProduct)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion internal System.Windows.Forms.Label Label1; internal System.Windows.Forms.ComboBox ComboBoxCategory; internal System.Windows.Forms.DataGrid DataGridProduct; }}