using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace Ex12_01
{
    public partial class Form8 : Form
    {
        static int Num = 0;
        int Count = 0;
        SqlConnection con = new SqlConnection("server=(local);user id=myzf;pwd=123456;database=DataServer");


        private void Resultinfo(int j)
        {
            DataSet dsNew = DtReslut(j);      //定义DataSet对象实例
            //提取数据记表中的记录分别赋值于标签和文本框
            this.label4.Text = dsNew.Tables[0].Rows[0][0].ToString();
            this.textBox1.Text = dsNew.Tables[0].Rows[0][1].ToString();
            this.textBox2.Text = dsNew.Tables[0].Rows[0][2].ToString();
            this.dateTimePicker1.Value = Convert.ToDateTime(dsNew.Tables[0].Rows[0][3].ToString());
        }

        private DataSet DtReslut(int i)
        {
            using (SqlDataAdapter da = new SqlDataAdapter())
            {
                da.SelectCommand = new SqlCommand("select * from consume", con);
                DataSet ds = new DataSet();
                da.Fill(ds, i, i + 1, "consume");
                return ds;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Num = 0;
            Resultinfo(Num);       //调用自定义函数Resultinfo
        }

        public Form8()
        {
            InitializeComponent();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除该条记录吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(" delete  from  consume where spendId =" + Convert.ToInt32(this.label4.Text), con);
                cmd.Connection = con;
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("成功删除数据!");
                Num = 0;
                Resultinfo(Num);       //调用自定义函数Resultinfo
            }
        }

        private void Form8_Load(object sender, EventArgs e)
        {
            Resultinfo(Num);     //调用自定义函数Resultinfo 
            //利用Select语句查询数据库表中记录的个数
            using (SqlCommand cmd = new SqlCommand("select Count(*) from consume", con))
            {
                con.Open();
                Count = Convert.ToInt32(cmd.ExecuteScalar());
                con.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Num -= 1;            //变量Num的值减1
            if (Num >= 0)
            {
                Resultinfo(Num);    //调用自定义函数Resultinfo
            }
            else
            {
                MessageBox.Show("现已是第一条记录!");   //提示对话框
                Num = 0;
                return; ;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Num = 0;
            Resultinfo(Num);       //调用自定义函数Resultinfo
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Num += 1;            //变量Num的值加1 
            if (Num < Count)
            {
                Resultinfo(Num);   //调用自定义函数Resultinfo
            }
            else
            {
                MessageBox.Show("现已是最后一条记录!");
                Num = Count - 1;
                return;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Num = Count;
            Resultinfo(Num - 1);
        }
    }
}
