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

SQL:

CREATE   TABLE   [ 訂貨明細 ]  (
    
[ 訂單號碼 ]   [ int ]   NOT   NULL  ,
    
[ 產品編號 ]   [ int ]   NOT   NULL  ,
    
[ 單價 ]   [ money ]   NOT   NULL   CONSTRAINT   [ DF__訂貨明細__單價__00AA174D ]   DEFAULT  ( 0 ),
    
[ 數量 ]   [ smallint ]   NOT   NULL   CONSTRAINT   [ DF__訂貨明細__數量__019E3B86 ]   DEFAULT  ( 1 ),
    
[ 折扣 ]   [ real ]   NOT   NULL   CONSTRAINT   [ DF__訂貨明細__折扣__02925FBF ]   DEFAULT  ( 0 ),
    
[ upsize_ts ]   [ timestamp ]   NULL  ,
    
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   and   [ 折扣 ]   <=   1 ),
    
CONSTRAINT   [ CK 訂貨明細 單價 ]   CHECK  ( [ 單價 ]   >=   0 ),
    
CONSTRAINT   [ CK 訂貨明細 數量 ]   CHECK  ( [ 數量 ]   >   0 )
ON   [ PRIMARY ]
GO


CREATE   TABLE   [ 訂貨主檔 ]  (
    
[ 訂單號碼 ]   [ int ]   IDENTITY  ( 1 1 NOT   NULL  ,
    
[ 客戶編號 ]   [ nvarchar ]  ( 5 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NULL  ,
    
[ 員工編號 ]   [ int ]   NULL   CONSTRAINT   [ DF__訂貨主檔__員工編號__79FD19BE ]   DEFAULT  ( 0 ),
    
[ 訂單日期 ]   [ datetime ]   NULL  ,
    
[ 要貨日期 ]   [ datetime ]   NULL  ,
    
[ 送貨日期 ]   [ datetime ]   NULL  ,
    
[ 送貨方式 ]   [ int ]   NULL   CONSTRAINT   [ DF__訂貨主檔__送貨方式__7AF13DF7 ]   DEFAULT  ( 0 ),
    
[ 運費 ]   [ money ]   NULL   CONSTRAINT   [ DF__訂貨主檔__運費__7BE56230 ]   DEFAULT  ( 0 ),
    
[ 收貨人 ]   [ nvarchar ]  ( 40 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NULL  ,
    
[ 送貨地址 ]   [ nvarchar ]  ( 60 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NULL  ,
    
[ 送貨城市 ]   [ nvarchar ]  ( 15 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NULL  ,
    
[ 送貨行政區 ]   [ nvarchar ]  ( 15 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NULL  ,
    
[ 送貨郵遞區號 ]   [ nvarchar ]  ( 10 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NULL  ,
    
[ 送貨國家地區 ]   [ nvarchar ]  ( 15 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NULL  ,
    
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   [ 訂貨主檔_FK02 ]   FOREIGN   KEY  
    (
        
[ 員工編號 ]
    ) 
REFERENCES   [ 員工 ]  (
        
[ 員工編號 ]
    ) 
ON   DELETE   CASCADE    ON   UPDATE   CASCADE  
ON   [ PRIMARY ]
GO


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


 运行界面:

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;
using  System.Globalization;

namespace  ch1
{
    
public partial class BindingDemoForm15 : Form
    
{
        
// 資料集物件的類別層級建立
        DataSet ds = new DataSet();

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

        
// CurrencyManager 物件的類別層級宣告
        BindingManagerBase bmCustomers;
        BindingManagerBase bmOrders;
        
public BindingDemoForm15()
        
{
            InitializeComponent();
        }


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

            
// 建立一個連接字串
            string strConnection = "Server=(local);Database=ch1;uid=sa;pwd=";

            
// 建立一個查詢命令字串
            string strSql = "SELECT 客戶編號, 公司名稱, 連絡人, 電話 FROM 客戶 WHERE 客戶編號 IN (SELECT DISTINCT 客戶編號 FROM 訂貨主檔)";

            
// 建立一個資料連接
            SqlConnection myConnection = new SqlConnection(strConnection);

            
// 建立一個資料配接器以便針對資料來源執行 SELECT 陳述式來提取出要填入資料集的資料記錄
            SqlDataAdapter myAD = new SqlDataAdapter(strSql, myConnection);


            
// 將資料填入資料集
            myAD.Fill(ds, "客戶");

            
// 重新指定用來提取資料來源之資料記錄的 SELECT 陳述式
            myAD.SelectCommand.CommandText = "SELECT 訂貨主檔.客戶編號, 訂貨主檔.訂單號碼, 訂貨主檔.訂單日期, " + "訂貨明細.產品編號, 產品資料.產品, 訂貨明細.單價, 訂貨明細.數量, 訂貨明細.折扣, " + "(訂貨明細.單價 * 訂貨明細.數量 * (1 - 訂貨明細.折扣)) AS 訂貨金額 " + "FROM 訂貨主檔 " + "INNER JOIN 訂貨明細 " + "ON 訂貨主檔.訂單號碼 = 訂貨明細.訂單號碼 " + "INNER JOIN 產品資料 " + "ON 訂貨明細.產品編號 = 產品資料.產品編號 " + "ORDER BY 訂貨主檔.客戶編號";

            
// 將資料填入資料集內名稱為「訂單明細金額」的資料表
            myAD.Fill(ds, "訂單明細金額");

            
// 宣告用來將資料集內之「客戶」資料表連結至「訂單明細金額」資料表的父欄位與子欄位
            DataColumn ParentColumn;
            DataColumn ChildColumn;
            ParentColumn 
= ds.Tables["客戶"].Columns["客戶編號"];
            ChildColumn 
= ds.Tables["訂單明細金額"].Columns["客戶編號"];

            
// 建立用來連結「客戶」資料表與「訂單明細金額」資料表的 DataRelation 物件,其名稱為「每一位客戶的訂單」。
            rel = new DataRelation("每一位客戶的訂單", ParentColumn, ChildColumn);
            ds.Relations.Add(rel);

            
// 將 TextBox 控制項繫結至父資料表(亦即「客戶」資料表)的各個欄位
            TextBoxCustomerID.DataBindings.Add("Text", ds, "客戶.客戶編號");
            TextBoxCompanyName.DataBindings.Add(
"Text", ds, "客戶.公司名稱");
            TextBoxContact.DataBindings.Add(
"Text", ds, "客戶.連絡人");
            TextBoxPhone.DataBindings.Add(
"Text", ds, "客戶.電話");

            
// 將 TextBox 控制項繫結至 DataRelation 物件(亦即「每一位客戶的訂單」)的各個欄位
            TextBoxOrderID.DataBindings.Add("Text", ds, "客戶.每一位客戶的訂單.訂單號碼");
            TextBoxProductID.DataBindings.Add(
"Text", ds, "客戶.每一位客戶的訂單.產品編號");
            TextBoxProductName.DataBindings.Add(
"Text", ds, "客戶.每一位客戶的訂單.產品");
            TextBoxPrice.DataBindings.Add(
"Text", ds, "客戶.每一位客戶的訂單.單價");
            TextBoxQuantity.DataBindings.Add(
"Text", ds, "客戶.每一位客戶的訂單.數量");
            TextBoxDiscount.DataBindings.Add(
"Text", ds, "客戶.每一位客戶的訂單.折扣");

            
// 將 DateTimePicker 控制項繫結至 DataRelation 物件(亦即「每一位客戶的訂單」)的「訂單日期」欄位
            DateTimePickerOrderDate.DataBindings.Add("Value", ds, "客戶.每一位客戶的訂單.訂單日期");

            
// 建立一個新的 Binding 物件並替它加入 Parse 與 Format 事件的委派,
            
// 然後將此 Binding 物件加至名稱為 TextBoxAmount 之 TextBox 控制項的 BindingsCollection 中。
            
// 您務必在將 Binding 物件加至集合中之前就先加入事件委派,否則,除非資料來源
            
// 之 BindingManagerBase 的目前物件改變,將不會有任何格式化發生。
            Binding myBindingObject = new Binding("Text", ds, "客戶.每一位客戶的訂單.訂貨金額");
            myBindingObject.Format 
+= DecimalToCurrencyString;
            myBindingObject.Parse 
+= CurrencyStringToDecimal;
            TextBoxAmount.DataBindings.Add(myBindingObject);

            
// 取得代表「客戶」資料表的 CurrencyManager 物件
            bmCustomers = this.BindingContext[ds, "客戶"];

            
// 設定當引發 bmCustomers 的 PositionChanged 事件時便執行事件處理常式「客戶_PositionChanged」
            bmCustomers.PositionChanged += 客戶_PositionChanged;

            
// 取得代表 DataRelation 物件(亦即「每一位客戶的訂單」)的 CurrencyManager 物件
            bmOrders = this.BindingContext[ds, "客戶.每一位客戶的訂單"];

            
// 設定當引發 bmOrders 的 PositionChanged 事件時便執行事件處理常式「訂單_PositionChanged」
            bmOrders.PositionChanged += 訂單_PositionChanged;

            
// 設定資料記錄目前位置訊息的初值
            TextBoxCustomerPosition.Text = string.Format("客戶記錄:目前位置 {0} 總數 {1}", bmCustomers.Position + 1, bmCustomers.Count);
            TextBoxOrderPosition.Text 
= string.Format("訂單記錄:目前位置 {0} 總數 {1}", bmOrders.Position + 1, bmOrders.Count);

            myConnection.Close();
        }


        
// 此方法是 Format 事件的事件處理常式。每當控制項顯示一個新的數值,該數值便會由其原生型別
        
// 被轉換成一個貨幣格式的字串。ToString 方法使用格式化字元 "c" 將該數值轉換成一個貨幣格式的字串。
        private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
        
{

            
// 使用 DesiredType 來檢測是否能轉換成 String 型別
            if ((!object.ReferenceEquals(cevent.DesiredType, typeof(string))))
            
{
                
return// TODO: might not be correct. Was : Exit Sub
            }

            
// 使用 ToString 方法將數值轉換成貨幣格式的字串
            cevent.Value = Convert.ToDecimal(cevent.Value).ToString("c");
        }



        
// 此方法是 Parse 事件的事件處理常式。每當所顯示的數值改變時,Parse 事件就會被引發。
        
// 在此主要是將貨幣格式的字串轉換回 Decimal 型別。
        private void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent)
        
{

            
// 使用 DesiredType 來檢測是否能轉換成 Decimal 型別
            if ((!object.ReferenceEquals(cevent.DesiredType, typeof(decimal))))
            
{
                
return// TODO: might not be correct. Was : Exit Sub
            }


            
// 將貨幣格式的字串轉換回 Decimal 型別
            
//cevent.Value = decimal.Parse(cevent.Value.ToString, NumberStyles.Currency, null);
            cevent.Value = decimal.Parse(cevent.Value.ToString(), NumberStyles.Currency, null);
        }


        
protected void 客戶_PositionChanged(object sender, System.EventArgs e)
        
{
            TextBoxCustomerPosition.Text 
= string.Format("客戶記錄:目前位置 {0} 總數 {1}", bmCustomers.Position + 1, bmCustomers.Count);
            TextBoxOrderPosition.Text 
= string.Format("訂單記錄:目前位置 {0} 總數 {1}", bmOrders.Position + 1, bmOrders.Count);
        }


        
protected void 訂單_PositionChanged(object sender, System.EventArgs e)
        
{
            TextBoxOrderPosition.Text 
= string.Format("訂單記錄:目前位置 {0} 總數 {1}", bmOrders.Position + 1, bmOrders.Count);
        }


        
// 按下導覽父資料表的「第一筆」按鈕
        private void btnFirst_Click(object sender, System.EventArgs e)
        
{
            
// 將 Position 屬性設定成 0
            bmCustomers.Position = 0;
        }


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

        }


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

        }


        
// 按下導覽父資料表的「最後一筆」按鈕
        private void btnEnd_Click(object sender, System.EventArgs e)
        
{
            bmCustomers.Position 
= bmCustomers.Count - 1;
        }


        
// 按下導覽 DataRelation 物件的「第一筆」按鈕
        private void btnChildFirst_Click(object sender, System.EventArgs e)
        
{
            bmOrders.Position 
= 0;
        }


        
// 按下導覽 DataRelation 物件的「上一筆」按鈕
        private void btnChildBack_Click(object sender, System.EventArgs e)
        
{
            
if (bmOrders.Position > 0)
            
{
                bmOrders.Position 
-= 1;
            }

        }


        
// 按下導覽 DataRelation 物件的「下一筆」按鈕
        private void btnChildNext_Click(object sender, System.EventArgs e)
        
{
            
if (bmOrders.Position < bmOrders.Count - 1)
            
{
                bmOrders.Position 
+= 1;
            }

        }


        
// 按下導覽 DataRelation 物件的「最後一筆」按鈕
        private void btnChildEnd_Click(object sender, System.EventArgs e)
        
{
            bmOrders.Position 
= bmOrders.Count - 1;
        }

    }

}

窗体代码:

namespace  ch1
{
    
partial class BindingDemoForm15
    
{
        
/// <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 Label3;
        
internal System.Windows.Forms.Label Label2;
        
internal System.Windows.Forms.Label Label1;
        
internal System.Windows.Forms.TextBox TextBoxDiscount;
        
internal System.Windows.Forms.Label lblDiscount;
        
internal System.Windows.Forms.TextBox TextBoxQuantity;
        
internal System.Windows.Forms.Label lblQuantity;
        
internal System.Windows.Forms.TextBox TextBoxPrice;
        
internal System.Windows.Forms.Label lblPrice;
        
internal System.Windows.Forms.TextBox TextBoxProductName;
        
internal System.Windows.Forms.Label lblProductName;
        
internal System.Windows.Forms.TextBox TextBoxProductID;
        
internal System.Windows.Forms.Label lblProductID;
        
internal System.Windows.Forms.TextBox TextBoxOrderID;
        
internal System.Windows.Forms.Label lblOrderID;
        
internal System.Windows.Forms.TextBox TextBoxOrderPosition;
        
internal System.Windows.Forms.Button btnChildEnd;
        
internal System.Windows.Forms.Button btnChildNext;
        
internal System.Windows.Forms.Button btnChildBack;
        
internal System.Windows.Forms.Button btnChildFirst;
        
internal System.Windows.Forms.Label lblAmount;
        
internal System.Windows.Forms.Label lblOrderDate;
        
internal System.Windows.Forms.TextBox TextBoxAmount;
        
internal System.Windows.Forms.DateTimePicker DateTimePickerOrderDate;
        
internal System.Windows.Forms.Label lblPhone;
        
internal System.Windows.Forms.TextBox TextBoxPhone;
        
internal System.Windows.Forms.Label lblContact;
        
internal System.Windows.Forms.Label lblCompany;
        
internal System.Windows.Forms.Label lblCustomerID;
        
internal System.Windows.Forms.TextBox TextBoxCustomerID;
        
internal System.Windows.Forms.TextBox TextBoxContact;
        
internal System.Windows.Forms.TextBox TextBoxCompanyName;
        
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 TextBoxCustomerPosition;
        
internal System.Windows.Forms.Panel Panel1;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值