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

创建数据库:

请见:ado.net c#.net2005 From第一讲(BindingDemoForm2)

运行界面:

 

BindingDemoForm3.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 BindingDemoForm3 : Form
    
{
        
public BindingDemoForm3()
        
{
            InitializeComponent();
        }

        DataSet ds 
= new DataSet();
        
private void BindingDemoForm3_Load(object sender, EventArgs e)
        
{
            
            
// 設定表單的最小大小
            this.MinimumSize = new Size(512542);

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

            
// 建立一個查詢命令字串
            string strSql = "SELECT 身份證字號,姓名,員工性別,住家地址,出生日期,目前薪資,部門,自傳 FROM 飛狐工作室";

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

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

            
// 將資料填入資料集
            myCommand.Fill(ds, "飛狐工作室");

            
// 將 TextBox 控制項的 Text 屬性繫結至資料集 ds 內之「飛狐工作室」資料表的「身份證字號」欄位
            TextBoxID.DataBindings.Add("Text", ds, "飛狐工作室.身份證字號");
            
// 將 TextBox 控制項的 Text 屬性繫結至資料集 ds 內之「飛狐工作室」資料表的「姓名」欄位
            TextBoxName.DataBindings.Add("Text", ds, "飛狐工作室.姓名");
            
// 將 CheckBox 控制項的 Checked 屬性繫結至資料集 ds 內之「飛狐工作室」資料表的「員工性別」欄位
            CheckBoxGender.DataBindings.Add("Checked", ds, "飛狐工作室.員工性別");
            
// 將 TextBox 控制項的 Text 屬性繫結至資料集 ds 內之「飛狐工作室」資料表的「住家地址」欄位
            TextBoxAddress.DataBindings.Add("Text", ds, "飛狐工作室.住家地址");
            
// 將 DateTimePicker 控制項的 Value 屬性繫結至資料集 ds 內之「飛狐工作室」資料表的「出生日期」欄位
            DateTimePickerBirthday.DataBindings.Add("Value", ds, "飛狐工作室.出生日期");
            
// 將 NumericUpDown 控制項的 Value 屬性繫結至資料集 ds 內之「飛狐工作室」資料表的「目前薪資」欄位
            NumericUpDownSalary.DataBindings.Add("Value", ds, "飛狐工作室.目前薪資");
            
// 將 TextBox 控制項的 Text 屬性繫結至資料集 ds 內之「飛狐工作室」資料表的「部門」欄位
            TextBoxDepartment.DataBindings.Add("Text", ds, "飛狐工作室.部門");
            
// 將 TextBox 控制項的 Text 屬性繫結至資料集 ds 內之「飛狐工作室」資料表的「自傳」欄位
            TextBoxContent.DataBindings.Add("Text", ds, "飛狐工作室.自傳");

            
// 設定當引發 PositionChanged 事件時便執行事件處理常式 飛狐工作室_PositionChanged
            this.BindingContext[ds, "飛狐工作室"].PositionChanged += 飛狐工作室_PositionChanged;

            
// 設定資料記錄目前位置訊息的初值
            TextBoxPosition.Text = string.Format("資料記錄:目前位置 {0} 總數 {1}"this.BindingContext[ds, "飛狐工作室"].Position + 1this.BindingContext[ds, "飛狐工作室"].Count);

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


        
protected void 飛狐工作室_PositionChanged(object sender, System.EventArgs e)
        
{
            TextBoxPosition.Text 
= string.Format("資料記錄:目前位置 {0} 總數 {1}"this.BindingContext[ds, "飛狐工作室"].Position + 1this.BindingContext[ds, "飛狐工作室"].Count);
        }


        
// 按下「第一筆」按鈕
        private void btnFirst_Click(object sender, System.EventArgs e)
        
{
            
// 將 Position 屬性設定成 0
            this.BindingContext[ds, "飛狐工作室"].Position = 0;
        }


        
// 按下「上一筆」按鈕
        private void btnBack_Click(object sender, System.EventArgs e)
        
{
            
if (this.BindingContext[ds, "飛狐工作室"].Position > 0)
            
{
                
// 將 Position 屬性遞減 1
                this.BindingContext[ds, "飛狐工作室"].Position -= 1;
            }

        }


        
// 按下「下一筆」按鈕
        private void btnNext_Click(object sender, System.EventArgs e)
        
{
            
if (this.BindingContext[ds, "飛狐工作室"].Position < this.BindingContext[ds, "飛狐工作室"].Count - 1)
            
{
                
// 將 Position 屬性遞增 1
                this.BindingContext[ds, "飛狐工作室"].Position += 1;
            }

        }


        
// 按下「最後一筆」按鈕
        private void btnEnd_Click(object sender, System.EventArgs e)
        
{
            
this.BindingContext[ds, "飛狐工作室"].Position = this.BindingContext[ds, "飛狐工作室"].Count - 1;
        }

    }

}

 

BindingDemoForm3.Designer.cs

 

namespace  ch1
{
    
partial class BindingDemoForm3
    
{
        
/// <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 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.NumericUpDown NumericUpDownSalary;
        
internal System.Windows.Forms.Label lblContent;
        
internal System.Windows.Forms.TextBox TextBoxContent;
        
internal System.Windows.Forms.DateTimePicker DateTimePickerBirthday;
        
internal System.Windows.Forms.CheckBox CheckBoxGender;
        
internal System.Windows.Forms.Label lblDepartment;
        
internal System.Windows.Forms.Label lblSalary;
        
internal System.Windows.Forms.Label lblBirthday;
        
internal System.Windows.Forms.Label lblAddress;
        
internal System.Windows.Forms.Label Label1;
        
internal System.Windows.Forms.Label lblName;
        
internal System.Windows.Forms.Label lblID;
        
internal System.Windows.Forms.TextBox TextBoxDepartment;
        
internal System.Windows.Forms.TextBox TextBoxAddress;
        
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、付费专栏及课程。

余额充值