示例#1
0
        /// <summary>
        /// 点击 "添加" 按钮触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInsert_Click(object sender, EventArgs e)
        {
            string date = cboYear.Text + '-' + cboMonth.Text + '-' + cboDay.Text;
            string sql  = string.Format("insert into consume values('{0}','{1}','{2}',{3},'{4}')",
                                        date, cboType.Text, cboCategory.Text, txtJinE.Text.Trim(), txtBeizhu.Text.Trim());
            SqlCommand cmd = new SqlCommand(sql, DBHelper.connection);

            DBHelper.connection.Open();
            // 增删改的语句使用 ExecuteNonQuery()
            int rowCount = cmd.ExecuteNonQuery();

            // 添加成功弹出对话框让用户知道
            if (rowCount > 0)
            {
                MessageBox.Show("添加记录成功");
                mForm.fill();
            }
            else
            {
                MessageBox.Show("添加记录失败");
            }
            DBHelper.connection.Close();
        }
示例#2
0
        /// <summary>
        /// "查询"按钮触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChaXun_Click(object sender, EventArgs e)
        {
            // 拼接查询条件 sql 语句
            string condition = string.Empty;

            // 按时间段查询
            if (rdoDuan.Checked)
            {
                condition += string.Format("ConsumeDate>='{0}' and ConsumeDate<='{1}'",
                                           cboYear.Text + "-" + cboMonth.Text + "-" + cboDay.Text,
                                           cboEndYear.Text + "-" + cboEndMonth.Text + "-" + cboEndDay.Text);
            }
            // 按时间点查询
            else
            {
                condition += string.Format("ConsumeDate='{0}'",
                                           cboYear.Text + "-" + cboMonth.Text + "-" + cboDay.Text);
            }

            // 按类别查询
            if (condition != string.Empty)
            {
                string category = string.Empty;
                if (cbGongZi.Checked)
                {
                    category += string.Format("'{0}',", cbGongZi.Text);
                }
                if (cbQiTaRu.Checked)
                {
                    category += string.Format("'{0}',", cbQiTaRu.Text);
                }
                if (cbYuLe.Checked)
                {
                    category += string.Format("'{0}',", cbYuLe.Text);
                }
                if (cbCanYin.Checked)
                {
                    category += string.Format("'{0}',", cbCanYin.Text);
                }
                if (cbLvYou.Checked)
                {
                    category += string.Format("'{0}',", cbLvYou.Text);
                }
                if (cbGouWu.Checked)
                {
                    category += string.Format("'{0}',", cbGouWu.Text);
                }
                if (cbQiTaChu.Checked)
                {
                    category += string.Format("'{0}',", cbQiTaChu.Text);
                }
                // 上面的字符串结尾都带有逗号  所以在下面去掉
                string categoryFina = category.Substring(0, category.Length - 1);
                condition += string.Format(" and Category in({0})", categoryFina);

                // 按金额范围查询
                double minMoney = 0, maxMoney = 0;
                // 用户输入不为空的验证
                if (txtMin.Text.Trim() == string.Empty)
                {
                    txtMin.Text = "0";
                }
                if (txtMax.Text.Trim() == string.Empty)
                {
                    txtMax.Text = "0";
                }
                try
                {
                    minMoney = Convert.ToDouble(txtMin.Text.Trim());
                    maxMoney = Convert.ToDouble(txtMax.Text.Trim());
                }
                catch (FormatException ex)
                {
                    MessageBox.Show("请输入正确金额数值");
                    return;
                }
                if (minMoney > 0)
                {
                    condition += string.Format(" and ConsumeMoney>={0}", minMoney);
                }
                if (maxMoney > 0)
                {
                    condition += string.Format(" and ConsumeMoney<={0}", maxMoney);
                }

                // 备注的模糊搜索
                if (txtDescription.Text.Trim() != string.Empty)
                {
                    condition += string.Format(" and Description like '%{0}%'", txtDescription.Text.Trim());
                }
            }

            // 调用 MainForm 中的绑定方法
            //mainForm.bindData(condition);

            // 下面写的都是我自己的尝试罢了
            SqlCommand cmd = new SqlCommand(mainForm.bindData(condition), DBHelper.connection);

            DBHelper.connection.Open();
            int rowCounts = cmd.ExecuteNonQuery();

            if (rowCounts > 0)
            {
                MessageBox.Show("query success");
                mainForm.fill();
            }
            else
            {
                MessageBox.Show("query failure");
            }
            DBHelper.connection.Close();

            this.Close();
        }