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

        private void Form3_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=(local);user id=myzf;pwd=123456;database=DataServer");
            con.Open();
            SqlCommand com = new SqlCommand("select distinct(spendprocd) from consume", con);
            comboBox1.Items.Clear();

            SqlDataReader dr = com.ExecuteReader();
            comboBox1.Items.Clear();
            while (dr.Read())
            {
                comboBox1.Items.Add(dr[0].ToString());
            }
            dr.Close();
            con.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //生成连接数据库字符串
            string ConStr = "server=(local);user id=myzf;pwd=123456;database=DataServer";
            //定义SqlConnection对象实例  
            SqlConnection con = new SqlConnection(ConStr);
            //定义Select查询语句
            string SqlStr = "select spendprocd as 消费项目,spendMoney as 消费金额, spendDate as 消费日期  from consume where spendprocd='" + comboBox1.SelectedItem.ToString() + "'";
            SqlDataAdapter ada = new SqlDataAdapter(SqlStr, con);
            DataSet ds = new DataSet();    //定义DataSet对象实例
            ada.Fill(ds);
            //连接数据表格，显示数据  
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            con.Close();
            SqlConnection mycon = new SqlConnection("server=(local);user id=myzf;pwd=123456;database=DataServer");
            mycon.Open();
            SqlCommand cmd = new SqlCommand("select sum(spendMoney) from consume where spendprocd='" + comboBox1.SelectedItem.ToString() + "'", mycon);
            SqlDataReader drr = cmd.ExecuteReader();
            while (drr.Read())
            {
                textBox1.Text = String.Format(Convert.ToDouble(drr.GetValue(0)).ToString());
                textBox1.BackColor = Color.GreenYellow;
            }
        }
    }
}
