用VS2010写一个简单的记账工具(四)综合代码

阿贝云提供免费的虚拟主机和免费的云服务器,独享IP,高在线,免备案,永久免费续期

地址都是国内的,速度十分快,实测云服务器免费,空间蛮大。适合学习和个人试用。极力推荐

阿贝云还有 2G2H5M、15元/月 4G4H5M、25元/月 优惠云服务器 可以购买,很实惠的https://www.abeiyun.com
--------------------- 

至于申请到免费云服务器和虚拟主机以后,要干什么,请参考我的博客   https://blog.youkuaiyun.com/DLXG001
 

经过前面的步骤,小工具基本就成型了。

现在把整个代码贴出来,方便大家研究。哈哈

    

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.Text.RegularExpressions;
    using System.Data.OleDb;
    
    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
    LoadData();
    }
    
    private void LoadData()
    {
    dateTimePicker_ShiJian.Text = DateTime.Now.ToShortDateString();//把今天的日期显示在时间控件里,默认的就是今天,不用每次在选了。
    GlobalData.SQL_Str = "select * from " + GlobalData.Excel_Sheet;//从数据库中查询记录
    OleDbConnection conn = new OleDbConnection(GlobalData.DbConn_Str);//打开数据库连接
    try
    {
    OleDbDataAdapter DAa = new OleDbDataAdapter(GlobalData.SQL_Str, conn);//实例化OleDbDataAdapter来读取记录
    DataTable DTt = new DataTable();//创建DataTable
    DAa.Fill(DTt);//把OleDbDataAdapter结果赋值给DataTable
    dataGridView.DataSource = DTt;//把DataTable指定为dataGridView数据源
    GlobalData.Id = DTt.Rows.Count;//获取当前已有记录条数,也就是序号,下次写入前+1即可
    GlobalData.SQL_Str = "select sum(小计) from " + GlobalData.Excel_Sheet;//从Excel中统计小计列的和
    conn.Open();
    OleDbCommand cmd_TMDr = new OleDbCommand(GlobalData.SQL_Str, conn);//执行统计sql语句
    object TM = cmd_TMDr.ExecuteScalar();//用object来存储结果
    GlobalData.TotalMoney = int.Parse(TM.ToString());//把合计结果赋值给全局变量
    conn.Close();
    label_TotalMoney.Text = "¥" + GlobalData.TotalMoney.ToString();//把合计结果显示到label中
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    finally
    {
    DataSet Ds = new DataSet();
    GlobalData.SQL_Str = "select 产品 from" + GlobalData.Excel_Sheet + "group by 产品";
    OleDbDataAdapter da = new OleDbDataAdapter(GlobalData.SQL_Str, conn);
    da.Fill(Ds, "产品");
    comboBox_ChanPin.DataSource = Ds.Tables["产品"];
    comboBox_ChanPin.DisplayMember = "产品";
    HuoQuDanJia();
    }
    }
    
    private void HuoQuDanJia()
    {
    if (comboBox_ChanPin.Text != "System.Data.DataRowView")
    {
    //获取产品单价NLRR
    GlobalData.SQL_Str = "select 单价 from " + GlobalData.Excel_Sheet + " where 产品= '" + comboBox_ChanPin.Text + "'";
    OleDbConnection conn = new OleDbConnection(GlobalData.DbConn_Str);
    conn.Open();
    OleDbCommand cmd_TMDr = new OleDbCommand(GlobalData.SQL_Str, conn);
    object DJ = cmd_TMDr.ExecuteScalar();
    textBox_DanJia.Text = DJ.ToString();
    conn.Close();
    return;
    }
    
    
    }
    
    private void textBox_ShuLiang_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (Char.IsControl(e.KeyChar))
    return;
    if (Char.IsDigit(e.KeyChar) && ((e.KeyChar & 0xFF) == e.KeyChar))
    return;
    e.Handled = true;
    }
    
    private void textBox_DanJia_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (Char.IsControl(e.KeyChar))
    return;
    if (Char.IsDigit(e.KeyChar) && ((e.KeyChar & 0xFF) == e.KeyChar))
    return;
    e.Handled = true;
    }
    
    private void textBox_ShuLiang_TextChanged(object sender, EventArgs e)
    {
    textBox_DanJia_TextChanged(sender, e);
    }
    
    private void textBox_DanJia_TextChanged(object sender, EventArgs e)
    {
    if (textBox_ShuLiang.Text != "" && textBox_DanJia.Text != "")
    {
    int temp = 0;
    temp = int.Parse(textBox_ShuLiang.Text.Trim()) * int.Parse(textBox_DanJia.Text.Trim());
    textBox_XiaoJi.Text = temp.ToString();
    }
    }
    
    private void button_XinZeng_Click(object sender, EventArgs e)
    {
    OleDbConnection conn = new OleDbConnection(GlobalData.DbConn_Str);//打开excel数据库
    int tempid = GlobalData.Id + 1;
    string tempDate = dateTimePicker_ShiJian.Value.Year.ToString() + "年";
    tempDate += dateTimePicker_ShiJian.Value.Month.ToString() + "月";
    tempDate += dateTimePicker_ShiJian.Value.Day.ToString() + "日";
    GlobalData.SQL_Str = "insert into " + GlobalData.Excel_Sheet + " values (" + tempid + ",'" + comboBox_ChanPin.Text + "'," + int.Parse(textBox_ShuLiang.Text.Trim()) + "," + int.Parse(textBox_DanJia.Text.Trim()) + "," + int.Parse(textBox_XiaoJi.Text.Trim()) + ",'" + textBox_JingShouRen.Text + "','" + textBox_SongHuoRen.Text + "','" + tempDate + "')";
    OleDbCommand cmd = new OleDbCommand(GlobalData.SQL_Str, conn);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    MessageBox.Show("添加成功!该条目序号为:【" + tempid.ToString() + "】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    LoadData();
    }
    
    private void comboBox_ChanPin_SelectedIndexChanged(object sender, EventArgs e)
    {
    if (comboBox_ChanPin.Text != "1035硒鼓")
    {
    HuoQuDanJia();
    }
    }
    
    private void button_ChongZhi_Click(object sender, EventArgs e)
    {
    comboBox_ChanPin.Text = "";
    textBox_ShuLiang.Text = "";
    textBox_DanJia.Text = "";
    textBox_JingShouRen.Text = "";
    textBox_SongHuoRen.Text = "";
    }
    }
    }


    
Excel数据库下载:点我下载

程序源代码下载:点我下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值