数据集的使用实例

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;


namespace HomeWorkTwoPrj
{

    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.TextBox textBox4;
        private System.Windows.Forms.TextBox textBox5;
        private System.Windows.Forms.Button btnFirst;
        private System.Windows.Forms.Button btnLast;
        private System.Windows.Forms.Button btnUp;
        private System.Windows.Forms.Button btnDown;

        private System.ComponentModel.Container components = null;
        //
        SqlConnection cn = null;
        DataSet ds = null;
        int temp = 0;

        public Form1()
        {

            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体代码
        // </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.textBox5 = new System.Windows.Forms.TextBox();
            this.btnFirst = new System.Windows.Forms.Button();
            this.btnDown = new System.Windows.Forms.Button();
            this.btnLast = new System.Windows.Forms.Button();
            this.btnUp = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
            this.label1.Location = new System.Drawing.Point(72, 32);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(184, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "显示员工信息";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // label2
            //
            this.label2.Location = new System.Drawing.Point(24, 72);
            this.label2.Name = "label2";
            this.label2.TabIndex = 1;
            this.label2.Text = "员工编号:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // label3
            //
            this.label3.Location = new System.Drawing.Point(24, 112);
            this.label3.Name = "label3";
            this.label3.TabIndex = 2;
            this.label3.Text = "员工姓名:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // label4
            //
            this.label4.Location = new System.Drawing.Point(24, 152);
            this.label4.Name = "label4";
            this.label4.TabIndex = 3;
            this.label4.Text = "员工性别:";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // label5
            //
            this.label5.Location = new System.Drawing.Point(24, 192);
            this.label5.Name = "label5";
            this.label5.TabIndex = 4;
            this.label5.Text = "公司名称:";
            this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // label6
            //
            this.label6.Location = new System.Drawing.Point(24, 232);
            this.label6.Name = "label6";
            this.label6.TabIndex = 5;
            this.label6.Text = "联系地址:";
            this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(128, 72);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(144, 21);
            this.textBox1.TabIndex = 6;
            this.textBox1.Text = "";
            //
            // textBox2
            //
            this.textBox2.Location = new System.Drawing.Point(128, 112);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(144, 21);
            this.textBox2.TabIndex = 7;
            this.textBox2.Text = "";
            //
            // textBox3
            //
            this.textBox3.Location = new System.Drawing.Point(128, 152);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(144, 21);
            this.textBox3.TabIndex = 8;
            this.textBox3.Text = "";
            //
            // textBox4
            //
            this.textBox4.Location = new System.Drawing.Point(128, 192);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(144, 21);
            this.textBox4.TabIndex = 9;
            this.textBox4.Text = "";
            //
            // textBox5
            //
            this.textBox5.Location = new System.Drawing.Point(128, 232);
            this.textBox5.Name = "textBox5";
            this.textBox5.Size = new System.Drawing.Size(144, 21);
            this.textBox5.TabIndex = 10;
            this.textBox5.Text = "";
            //
            // btnFirst
            //
            this.btnFirst.Location = new System.Drawing.Point(40, 272);
            this.btnFirst.Name = "btnFirst";
            this.btnFirst.TabIndex = 11;
            this.btnFirst.Text = "首条数据";
            this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);
            //
            // btnDown
            //
            this.btnDown.Location = new System.Drawing.Point(200, 272);
            this.btnDown.Name = "btnDown";
            this.btnDown.TabIndex = 12;
            this.btnDown.Text = "下一条";
            this.btnDown.Click += new System.EventHandler(this.btnDown_Click);
            //
            // btnLast
            //
            this.btnLast.Location = new System.Drawing.Point(40, 312);
            this.btnLast.Name = "btnLast";
            this.btnLast.TabIndex = 11;
            this.btnLast.Text = "末条数据";
            this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
            //
            // btnUp
            //
            this.btnUp.Location = new System.Drawing.Point(200, 312);
            this.btnUp.Name = "btnUp";
            this.btnUp.TabIndex = 12;
            this.btnUp.Text = "上一条";
            this.btnUp.Click += new System.EventHandler(this.btnUp_Click);
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(328, 358);
            this.Controls.Add(this.btnDown);
            this.Controls.Add(this.btnFirst);
            this.Controls.Add(this.textBox5);
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnLast);
            this.Controls.Add(this.btnUp);
            this.Name = "Form1";
            this.Text = "随风飘逸";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            //禁用文本框
            this.textBox1.ReadOnly = true;
            this.textBox2.ReadOnly = true;
            this.textBox3.ReadOnly = true;
            this.textBox4.ReadOnly = true;
            this.textBox5.ReadOnly = true;
            //初始化连接
            cn = new SqlConnection("server = localhost;database = StudentDB;uid = sa;pwd =");
            //
            ds = new DataSet();
            //
            SqlDataAdapter sda = new SqlDataAdapter("select * from student", cn);
            //填充数据集
            sda.Fill(ds, "stu");
        }
        //首条数据按钮的事件
        private void btnFirst_Click(object sender, System.EventArgs e)
        {
            //清空
            this.textBox1.Clear();
            this.textBox2.Clear();
            this.textBox3.Clear();
            this.textBox4.Clear();
            this.textBox5.Clear();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (i == 0)
                {
                    //绑定数据集
                    this.textBox1.Text = ds.Tables[0].Rows[i][0].ToString();
                    this.textBox2.Text = ds.Tables[0].Rows[i][1].ToString();
                    this.textBox3.Text = ds.Tables[0].Rows[i][2].ToString();
                    this.textBox4.Text = ds.Tables[0].Rows[i][3].ToString();
                    this.textBox5.Text = ds.Tables[0].Rows[i][4].ToString();
                }
            }
        }
        //末条数据按钮的事件
        private void btnLast_Click(object sender, System.EventArgs e)
        {
            //清空
            this.textBox1.Clear();
            this.textBox2.Clear();
            this.textBox3.Clear();
            this.textBox4.Clear();
            this.textBox5.Clear();

            for (int i = 0; i <= ds.Tables[0].Rows.Count; i++)
            {
                if (i == ds.Tables[0].Rows.Count - 1)
                {
                    //绑定数据集
                    this.textBox1.Text = ds.Tables[0].Rows[i][0].ToString();
                    this.textBox2.Text = ds.Tables[0].Rows[i][1].ToString();
                    this.textBox3.Text = ds.Tables[0].Rows[i][2].ToString();
                    this.textBox4.Text = ds.Tables[0].Rows[i][3].ToString();
                    this.textBox5.Text = ds.Tables[0].Rows[i][4].ToString();
                }
            }
        }
        //下一条记录按钮的事件
        private void btnDown_Click(object sender, System.EventArgs e)
        {
            //通过下标做相应的判断
            if (this.temp < ds.Tables[0].Rows.Count - 1)
            {
                this.temp++;
            }
            else
            {
                //     MessageBox.Show("数据集内已经没有数据拉!");
                //     //如果已经把数据读完,将给下标服赋初值,从头开始
                //     this.temp = 0;
                return;
            }
            //清空
            this.textBox1.Clear();
            this.textBox2.Clear();
            this.textBox3.Clear();
            this.textBox4.Clear();
            this.textBox5.Clear();
            //绑定数据集
            this.textBox1.Text = ds.Tables[0].Rows[temp][0].ToString();
            this.textBox2.Text = ds.Tables[0].Rows[temp][1].ToString();
            this.textBox3.Text = ds.Tables[0].Rows[temp][2].ToString();
            this.textBox4.Text = ds.Tables[0].Rows[temp][3].ToString();
            this.textBox5.Text = ds.Tables[0].Rows[temp][4].ToString();
        }
        //上条数据按钮的事件
        private void btnUp_Click(object sender, System.EventArgs e)
        {
            //判断
            if (this.temp > 0)
            {
                this.temp--;
            }
            //清空
            this.textBox1.Clear();
            this.textBox2.Clear();
            this.textBox3.Clear();
            this.textBox4.Clear();
            this.textBox5.Clear();
            //绑定数据集
            this.textBox1.Text = ds.Tables[0].Rows[temp][0].ToString();
            this.textBox2.Text = ds.Tables[0].Rows[temp][1].ToString();
            this.textBox3.Text = ds.Tables[0].Rows[temp][2].ToString();
            this.textBox4.Text = ds.Tables[0].Rows[temp][3].ToString();
            this.textBox5.Text = ds.Tables[0].Rows[temp][4].ToString();
        }
    }
}

 

转载于:https://www.cnblogs.com/bobby7514650/archive/2009/02/24/1396969.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值