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

数据库:

CREATE   TABLE   [ 人員健康 ]  (
    
[ 員工編號 ]   [ int ]   IDENTITY  ( 1 1 NOT   NULL  ,
    
[ 姓名 ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NULL  ,
    
[ 性別 ]   [ bit ]   NOT   NULL   CONSTRAINT   [ DF__人員健康__性別__0880433F ]   DEFAULT  ( 0 ),
    
[ 出生日期 ]   [ datetime ]   NULL  ,
    
[ 血型 ]   [ nvarchar ]  ( 10 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NULL  ,
    
CONSTRAINT   [ aaaaa人員健康_PK ]   PRIMARY   KEY    NONCLUSTERED  
    (
        
[ 員工編號 ]
    )  
ON   [ PRIMARY ]  
ON   [ PRIMARY ]
GO


CREATE   TABLE   [ BloodType ]  (
    
[ Id ]   [ smallint ]   NOT   NULL  ,
    
[ Type ]   [ nvarchar ]  ( 10 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NULL  ,
    
[ Characteristic ]   [ nvarchar ]  ( 50 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NULL  ,
    
CONSTRAINT   [ PK_BloodType ]   PRIMARY   KEY    CLUSTERED  
    (
        
[ Id ]
    )  
ON   [ PRIMARY ]  
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 BindingDemoForm7 : Form
    
{
        
public BindingDemoForm7()
        
{
            InitializeComponent();
        }

        
// 資料集物件的類別層級建立
        DataSet ds = new DataSet();

        
// CurrencyManager 物件的類別層級宣告
        BindingManagerBase bmEmployeeHealth;
        
private void BindingDemoForm7_Load(object sender, System.EventArgs e)
        
{
            
// 設定表單的最小大小
            this.MinimumSize = new Size(627457);

            
// 建立一個連接字串
            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 Type, Characteristic FROM BloodType";

            
// 將資料填入資料集內名稱為 BloodType 的資料表
            myAD.Fill(ds, "BloodType");

            
// 將 TextBox 控制項的 Text 屬性繫結至資料集 ds 內之「人員健康」資料表的「員工編號」欄位
            TextBoxID.DataBindings.Add("Text", ds, "人員健康.員工編號");
            
// 將 TextBox 控制項的 Text 屬性繫結至資料集 ds 內之「人員健康」資料表的「姓名」欄位
            TextBoxName.DataBindings.Add("Text", ds, "人員健康.姓名");
            
// 將 CheckBox 控制項的 Checked 屬性繫結至資料集 ds 內之「人員健康」資料表的「性別」欄位
            CheckBoxGender.DataBindings.Add("Checked", ds, "人員健康.性別");
            
// 將 DateTimePicker 控制項的 Value 屬性繫結至資料集 ds 內之「人員健康」資料表的「出生日期」欄位
            DateTimePickerBirthday.DataBindings.Add("Value", ds, "人員健康.出生日期");

            
// 將 ComboBox 控制項的資料來源設定成資料集 ds 內的 BloodType 資料表
            ComboBoxBloodType.DataSource = ds.Tables["BloodType"];
            
// 將 ComboBox 控制項的 DisplayMember 屬性設定成 BloodType 資料表的 Characteristic 欄位
            ComboBoxBloodType.DisplayMember = "Characteristic";
            
// 將 ComboBox 控制項的 ValueMember 屬性設定成 BloodType 資料表的 Type 欄位
            ComboBoxBloodType.ValueMember = "Type";

            
// 將 SelectedValue 屬性繫結至「人員健康」資料表的「血型」欄位,如此一來,
            
// 使用者所選取之選項的 Type 欄位內容便會寫入「人員健康」資料表的「血型」欄位。
            ComboBoxBloodType.DataBindings.Add("SelectedValue", ds, "人員健康.血型");

            
// 取得代表「人員健康」資料表的 CurrencyManager 物件
            bmEmployeeHealth = this.BindingContext[ds, "人員健康"];

            
// 設定當引發 PositionChanged 事件時便執行事件處理常式 人員健康_PositionChanged
            bmEmployeeHealth.PositionChanged += 人員健康_PositionChanged;

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

            
// 關閉對資料庫的連接
            myConnection.Close();
        }

        
// 更新資料記錄目前位置的訊息
        protected void 人員健康_PositionChanged(object sender, System.EventArgs e)
        
{
            TextBoxPosition.Text 
= string.Format("資料記錄:目前位置 {0} 總數 {1}", bmEmployeeHealth.Position + 1, bmEmployeeHealth.Count);
        }


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


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

        }


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

        }


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

    }

}

窗体代码:

 

namespace  ch1
{
    
partial class BindingDemoForm7
    
{
        
/// <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.ComboBox ComboBoxBloodType;
        
internal System.Windows.Forms.Label lblHeader;
        
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.DateTimePicker DateTimePickerBirthday;
        
internal System.Windows.Forms.CheckBox CheckBoxGender;
        
internal System.Windows.Forms.Label lblBloodType;
        
internal System.Windows.Forms.Label lblBirthday;
        
internal System.Windows.Forms.Label Label1;
        
internal System.Windows.Forms.Label lblName;
        
internal System.Windows.Forms.Label lblID;
        
internal System.Windows.Forms.TextBox TextBoxName;
        
internal System.Windows.Forms.TextBox TextBoxID;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值