ado.net c#.net2005 From第一讲(BindingDemoForm12)

来源:互联网 发布:校园网络文化活动方案 编辑:程序博客网 时间:2024/06/05 08:20

SQL:见上

运行界面:

 

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 BindingDemoForm12 : Form
    
{
        
// 資料集物件的類別層級建立
        DataSet ds = new DataSet();

        
// 關聯性連結物件的類別層級宣告
        DataRelation rel;

        
// CurrencyManager 物件的類別層級宣告
        BindingManagerBase bmCategory;
        
public BindingDemoForm12()
        
{
            InitializeComponent();
        }


        
private void BindingDemoForm12_Load(object sender, System.EventArgs e)
        
{
            
// 設定表單的最小大小
            this.MinimumSize = new Size(680616);

            
// 建立一個連接字串
            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);

            TextBoxCategoryID.DataBindings.Add(
"Text", ds, "產品類別.類別編號");
            TextBoxCategoryName.DataBindings.Add(
"Text", ds, "產品類別.類別名稱");
            TextBoxDescription.DataBindings.Add(
"Text", ds, "產品類別.說明");

            
// 將 DataGrid 控制項繫結至 DataRelation 物件(亦即「每一種類別的產品」)
            DataGridProduct.SetDataBinding(ds, "產品類別.每一種類別的產品");

            
// 取得代表「產品類別」資料表的 CurrencyManager 物件
            bmCategory = this.BindingContext[ds, "產品類別"];

            
// 設定當引發 PositionChanged 事件時便執行事件處理常式 產品類別_PositionChanged
            bmCategory.PositionChanged += 產品類別_PositionChanged;

            
// 設定資料記錄目前位置訊息的初值
            TextBoxPosition.Text = string.Format("產品類別記錄:目前位置 {0} 總數 {1}", bmCategory.Position + 1, bmCategory.Count);

            myConnection.Close();

        }


        
protected void 產品類別_PositionChanged(object sender, System.EventArgs e)
        
{
            TextBoxPosition.Text 
= string.Format("產品類別記錄:目前位置 {0} 總數 {1}", bmCategory.Position + 1, bmCategory.Count);
        }


        
// 按下「第一筆」按鈕
        private void btnFirst_Click(object sender, System.EventArgs e)
        
{
            
// 將 Position 屬性設定成 0
            bmCategory.Position = 0;
        }


        
// 按下「上一筆」按鈕
        private void btnBack_Click(object sender, System.EventArgs e)
        
{
            
if (bmCategory.Position > 0)
            
{
                
// 將 Position 屬性遞減 1
                bmCategory.Position -= 1;
            }

        }


        
// 按下「下一筆」按鈕
        private void btnNext_Click(object sender, System.EventArgs e)
        
{
            
if (bmCategory.Position < bmCategory.Count - 1)
            
{
                
// 將 Position 屬性遞增 1
                bmCategory.Position += 1;
            }

        }


        
// 按下「最後一筆」按鈕
        private void btnEnd_Click(object sender, System.EventArgs e)
        
{
            bmCategory.Position 
= bmCategory.Count - 1;
        }

    }

}

窗体代码:

 

namespace ch1
{
    
partial class BindingDemoForm12
    
{
        
/// <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 窗体设计器生成的代码

        
internal System.Windows.Forms.Label lblSupplierID;
        
internal System.Windows.Forms.Label lblProduct;
        
internal System.Windows.Forms.Label lblProductID;
        
internal System.Windows.Forms.TextBox TextBoxCategoryID;
        
internal System.Windows.Forms.TextBox TextBoxDescription;
        
internal System.Windows.Forms.TextBox TextBoxCategoryName;
        
internal System.Windows.Forms.Button btnEnd;
        
internal System.Windows.Forms.Button btnNext;
        
internal System.Windows.Forms.Button btnBack;
        
internal System.Windows.Forms.Button btnFirst;
        
internal System.Windows.Forms.TextBox TextBoxPosition;
        
internal System.Windows.Forms.DataGrid DataGridProduct;
    }

}