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 Ex04_8
{
    public partial class Form1 : Form
    {
        static int Num = 0;
        int Count = 0;
        SqlConnection con = new SqlConnection("server=(local);user id=myzf;pwd=123456;database=MysqlData");

        private void Resultinfo(int j)
        {
            DataSet dsNew = DtReslut(j);
            this.label5.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.textBox3.Text = dsNew.Tables[0].Rows[0][3].ToString();
            this.textBox4.Text = dsNew.Tables[0].Rows[0][4].ToString();
        }

        private DataSet DtReslut(int i)
        {
            using (SqlDataAdapter da = new SqlDataAdapter())
            {
                da.SelectCommand = new SqlCommand("select * from 应付账", con);
                DataSet ds = new DataSet();
                da.Fill(ds, i, i + 1, "应付账");
                return ds;
            }
        }


        public Form1()
        {
            InitializeComponent();
        }

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

        private void button5_Click(object sender, EventArgs e)
        {
            Num -= 1;
            if (Num >= 0)
            {
                Resultinfo(Num);
            }
            else
            {
                MessageBox.Show("现已是第一条记录!");
                Num = 0;
                return; ;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Num += 1;
            if (Num < Count)
            {
                Resultinfo(Num);
            }
            else
            {
                MessageBox.Show("现已是最后一条记录!");
                Num = Count - 1;
                return;
            }
        }

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

        private void Form1_Load(object sender, EventArgs e)
        {
            Resultinfo(Num);
            using (SqlCommand cmd = new SqlCommand("select Count(*) from 应付账", con))
            {
                con.Open();
                Count = Convert.ToInt32(cmd.ExecuteScalar());
                con.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                con.Open();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "proc_Update";
                SqlParameter[] par ={ 
                    new SqlParameter("@id",this.label5 .Text ),
                    new SqlParameter("@name",this.textBox1.Text), 
                    new SqlParameter("@money",this.textBox2.Text),
                    new SqlParameter("@moneyed",this.textBox3.Text),
                    new SqlParameter("@moneying",this.textBox4.Text)
                };
                foreach (SqlParameter parms in par)
                {
                    cmd.Parameters.Add(parms);
                }
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("已成功修改一条数据库记录！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 myf = new Form2();      //定义Form2窗体实例
            myf.Show();                //显示窗体
        }
    }
}
