天然博客

c#大作业与心理感想

2023-12-14 11:48:08 140

文章目录

        

大作业

心理感想

  • 现在的水平,写这样的大作业,确实简单,换句话讲,如果你写了3年代码,这样的作业还
    能卡住的话,那是比较不显示现实的。
  • 在临大和在港湾不一样了,以前的心境是,大作业我要做到最好,要让老师眼前一亮,确实
    环境不一样了,所以说以管窥豹,当一个体制,你没有办法,再往前走一步了,可能你就不上
    心了。当然也与自己所看有关,初入港湾时,我还是个对编程没有一点认识的家伙,现在感觉
    自己有点水平了,感觉,课上教的不太行(方向不太行),所以跟着课堂的脚步了,自己安排
    自己的编程之路了,其实从港湾也是这样,只不过,不太严重。
  • 总体来说随便一写,客户端这个东西,特别时桌面编程,凉的不能再凉了。

代码

  • Form1.cs
using System;
using System.Collections;
using System.Windows.Forms;

namespace AccountSystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void doLogin_Click(object sender, EventArgs e)
        {
            string username = userNameBox.Text;
            string password = passwordBox.Text;
            if (TextUtils.isEmpty(username)) {
                MessageBox.Show("用户名不能为空");
                return;
            }
            if (TextUtils.isEmpty(password)) {
                MessageBox.Show("密码不能为空");
                return;
            }
            string sql = String.Format($"select * from tb_user where username = '{username}' and password = '{password}'");
            Hashtable result = SqlUtils.commitQueryUser(sql);
            Constants.CURRENT_USER_ID = int.Parse(result["id"].ToString());
            if (result.Count == 0)
            {
                MessageBox.Show("用户名或密码错误");
                return;
            }
            else { 
                this.Hide();
                FormAccount formAccount = new FormAccount();
                formAccount.Show();
            }
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormRegister formRegister = new FormRegister();
            formRegister.ShowDialog();
        }
    }
}
  • FormAccount.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace AccountSystem
{
    public partial class FormAccount : Form
    {
        public FormAccount()
        {
            InitializeComponent();
        }

        public void FormAccount_Load(object sender, EventArgs e)
        {
            string sql = String.Format($"select a.id, a.title, a.money, a.remark, t.name as type, u.username, a.create_time, a.type_id from tb_account as a, tb_type as t, tb_user as u where a.type_id = t.id and a.user_id = u.id and u.id = {Constants.CURRENT_USER_ID}");
            Hashtable result =SqlUtils.commitQueryAccount( sql );
            ArrayList idList = (ArrayList)result["id"];
            ArrayList titleList = (ArrayList)result["title"];
            ArrayList moneyList = (ArrayList)result["money"];
            ArrayList remarkList = (ArrayList)result["remark"];
            ArrayList typeList = (ArrayList)result["type"];
            ArrayList userList = (ArrayList)result["username"];
            ArrayList timeList = (ArrayList)result["createTime"];
            ArrayList typeIdList = (ArrayList)result["typeId"];
            for (int i = 0; i < timeList.Count; i++)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[i].Cells[0].Value = idList[i];
                dataGridView1.Rows[i].Cells[1].Value = titleList[i];
                dataGridView1.Rows[i].Cells[2].Value = moneyList[i];
                dataGridView1.Rows[i].Cells[3].Value = remarkList[i];
                dataGridView1.Rows[i].Cells[4].Value = typeList[i];
                dataGridView1.Rows[i].Cells[5].Value = userList[i];
                dataGridView1.Rows[i].Cells[6].Value = timeList[i];
                dataGridView1.Rows[i].Cells[7].Value = typeIdList[i];
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string text = "添加账单";
            FormAddAccount formAddAccount = new FormAddAccount(this, null, text, true);
            formAddAccount.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string item = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string sql = String.Format($"delete from tb_account where id = {int.Parse(item)}");
            int count = SqlUtils.commitDelAccount(sql);
            if (count > 0)
            {
                MessageBox.Show("删除成功");
                dataGridView1.Rows.Clear();
                this.FormAccount_Load(sender, e);
            }
            else {
                MessageBox.Show("删除失败");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string text = "编辑账单";
            DataGridViewRow item = dataGridView1.SelectedRows[0];
            FormAddAccount formAddAccount = new FormAddAccount(this, item, text, false);
            formAddAccount.Show();

        }
    }
}

  • FormAddAccount.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace AccountSystem
{
    public partial class FormAddAccount : Form
    {
        private FormAccount formAccount;

        private DataGridViewRow item;

        private string typeText;

        // true为添加操作 false为更新操作
        private bool sign;

        public FormAddAccount(FormAccount formAccount, DataGridViewRow item, string typeText, bool sign)
        {
            this.formAccount = formAccount;
            this.item = item;
            this.sign = sign;
            this.typeText = typeText;
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 检查数据
            string title = titleBox.Text;
            if (TextUtils.isEmpty(title))
            {
                MessageBox.Show("标题不能为空");
                return;
            }
            string money = moneyBox.Text;
            if (TextUtils.isEmpty(money)) {
                MessageBox.Show("金额不能为空");
                return;
            }
            string remark = remarkBox.Text;
            if (TextUtils.isEmpty(remark))
            {
                MessageBox.Show("备注不能为空");
            }
            string type = typeBox.SelectedValue.ToString();
            DateTime createTime = timeBox.Value;
            // 保存到数据库
            // 判断类型是添加操作还是更新操作
            int count = 0;
            if (sign)
            {
                string sql = String.Format($"insert into tb_account(title, money, remark, type_id, user_id, create_time) values('{title}', {float.Parse(money)}, '{remark}', {int.Parse(type)}, {Constants.CURRENT_USER_ID}, '{createTime}')");
                count = SqlUtils.commitInsertAccount(sql);
            }
            else {
                int id = int.Parse(item.Cells[0].Value.ToString());
                string sql = String.Format($"update tb_account set title = '{title}', money = {float.Parse(money)}, remark = '{remark}', type_id = {int.Parse(type)}, create_time = '{createTime}' where id = {id}");
                count = SqlUtils.commitUpdateAccount(sql);
            }

            if (count > 0)
            {
                // 清空原来的表格
                formAccount.dataGridView1.Rows.Clear();
                formAccount.FormAccount_Load(sender, e);
                MessageBox.Show("成功");
            }
            else {
                MessageBox.Show("失败");
            }
        }

        private void FormAddAccount_Load(object sender, EventArgs e)
        {
            this.Text = typeText;
            button1.Text = typeText;

            string sql = "select id, name from tb_type";
            ArrayList result = SqlUtils.commitQueryType(sql);
            typeBox.DataSource = result;
            typeBox.DisplayMember = "Value";
            typeBox.ValueMember = "Key";
            // 更新操作回显数据
            if (!sign)
            {
                titleBox.Text = item.Cells[1].Value.ToString();
                moneyBox.Text = item.Cells[2].Value.ToString();
                remarkBox.Text = item.Cells[3].Value.ToString();
                timeBox.Text = item.Cells[6].Value.ToString();
                typeBox.SelectedValue = item.Cells[7].Value.ToString();
            }
        }
    }
}
  • FormRegister.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AccountSystem
{
    public partial class FormRegister : Form
    {
        public FormRegister()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (TextUtils.isEmpty(regNameBox.Text)) {
                MessageBox.Show("账号不能为空");
                return;
            }
            if (TextUtils.isEmpty(regPwdBox.Text)) {
                MessageBox.Show("密码不能为空");
                return;
            }
            // 提交数据
            string sql = String.Format($"insert into tb_user(username, password) values('{regNameBox.Text}', '{regPwdBox.Text}')");
            int count = SqlUtils.commitInsertUser(sql);
            if (count > 0)
            {
                MessageBox.Show("注册成功");
                this.Close();
            }
            else {
                MessageBox.Show("注册失败");
            }
        }
    }
}
  • SqlUtils.cs
using System;
using System.Collections;
using System.Drawing;
using MySql.Data.MySqlClient;

namespace AccountSystem
{
    internal class SqlUtils
    {
        private static string connStr = "server=192.168.226.128;user id=root; password=123456; database=account_system; port=3306; Charset=utf8";

        public static Hashtable commitQueryAccount(string sql) {
            MySqlConnection connection = new MySqlConnection(connStr);
            MySqlDataReader reader = null;
            ArrayList idList = new ArrayList();
            ArrayList titleList = new ArrayList();
            ArrayList moneyList = new ArrayList();
            ArrayList remarkList = new ArrayList();
            ArrayList typeList = new ArrayList();
            ArrayList userList = new ArrayList();
            ArrayList timeList = new ArrayList();
            ArrayList typeIdList = new ArrayList();
            Hashtable result = new Hashtable();
            try
            {
                connection.Open();
                MySqlCommand command = new MySqlCommand(sql, connection);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    idList.Add(reader["id"].ToString());
                    titleList.Add(reader["title"].ToString());
                    moneyList.Add(reader["money"].ToString());
                    remarkList.Add(reader["remark"].ToString());
                    typeList.Add(reader["type"].ToString());
                    userList.Add(reader["username"].ToString());
                    timeList.Add(reader["create_time"].ToString());
                    typeIdList.Add(reader["type_id"].ToString());
                }
                result.Add("id", idList);
                result.Add("title", titleList);
                result.Add("money", moneyList);
                result.Add("remark", remarkList);
                result.Add("type", typeList);
                result.Add("username", userList);
                result.Add("createTime", timeList);
                result.Add ("typeId", typeIdList);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return result;
        }
        public static int commitInsertAccount(string sql) {
            int count = 0;
            MySqlConnection connection = new MySqlConnection(connStr);
            try
            {
                connection.Open();
                MySqlCommand command = new MySqlCommand(sql, connection);
                count = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return count;

        }

        public static Hashtable commitQueryUser(string sql) {
            MySqlConnection connection = new MySqlConnection(connStr);
            MySqlDataReader reader = null;
            Hashtable result = new Hashtable();
            try
            {
                connection.Open();
                MySqlCommand command = new MySqlCommand(sql, connection);
                reader =  command.ExecuteReader();
                if (reader.Read()) {
                    result.Add("id", reader["id"].ToString());
                    result.Add("username", reader["username"].ToString());
                    result.Add("password", reader["password"].ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally { 
                connection.Close();
            }
            return result;
        }

        public static ArrayList commitQueryType(string sql)
        {
            MySqlConnection connection = new MySqlConnection(connStr);
            MySqlDataReader reader = null;
            ArrayList result = new ArrayList();
            try
            {
                connection.Open();
                MySqlCommand command = new MySqlCommand(sql, connection);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    result.Add(new DictionaryEntry(reader["id"].ToString(), reader["name"].ToString()));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return result;
        }

        public static int commitDelAccount(string sql)
        {
            int count = 0;
            MySqlConnection connection = new MySqlConnection(connStr);
            try
            {
                connection.Open();
                MySqlCommand command = new MySqlCommand(sql, connection);
                count = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return count;
        }

        public static int commitUpdateAccount(string sql) {
            int count = 0;
            MySqlConnection connection = new MySqlConnection(connStr);
            try
            {
                connection.Open();
                MySqlCommand command = new MySqlCommand(sql, connection);
                count = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return count;
        }

        public static int commitInsertUser(string sql) {
            int count = 0;
            MySqlConnection connection = new MySqlConnection(connStr);
            try
            {
                connection.Open();
                MySqlCommand command = new MySqlCommand(sql, connection);
                count = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                connection.Close();
            }
            return count;
        }
    }
}
  • TextUtils.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AccountSystem
{
    internal class TextUtils
    {
        public static bool isEmpty(string text) {
            return text == "" || text == null;
        }
    }
}

sql文件

/*
 Navicat Premium Data Transfer

 Source Server         : account-mysql
 Source Server Type    : MySQL
 Source Server Version : 50736 (5.7.36)
 Source Host           : 192.168.226.128:3306
 Source Schema         : account_system

 Target Server Type    : MySQL
 Target Server Version : 50736 (5.7.36)
 File Encoding         : 65001

 Date: 14/12/2023 11:23:25
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for tb_account
-- ----------------------------
DROP TABLE IF EXISTS `tb_account`;
CREATE TABLE `tb_account`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(24) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `money` float NOT NULL,
  `remark` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `type_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `create_time` datetime NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of tb_account
-- ----------------------------
INSERT INTO `tb_account` VALUES (1, '吃饭', 198, '饭太好吃了', 1, 1, '2023-10-30 21:21:31');
INSERT INTO `tb_account` VALUES (2, '买笔', 6, '笔不好用', 4, 1, '2023-12-13 09:56:26');
INSERT INTO `tb_account` VALUES (6, '买衣服', 100, '很暖和', 3, 1, '2023-12-13 10:21:27');
INSERT INTO `tb_account` VALUES (7, '给天然钱', 800, '天然高兴了', 3, 2, '2023-11-27 11:19:09');
INSERT INTO `tb_account` VALUES (8, '再给天然钱', 900, '天然又高兴了', 1, 2, '2023-11-03 11:19:09');

-- ----------------------------
-- Table structure for tb_type
-- ----------------------------
DROP TABLE IF EXISTS `tb_type`;
CREATE TABLE `tb_type`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(24) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of tb_type
-- ----------------------------
INSERT INTO `tb_type` VALUES (1, '微信');
INSERT INTO `tb_type` VALUES (2, '支付宝');
INSERT INTO `tb_type` VALUES (3, '银行卡');
INSERT INTO `tb_type` VALUES (4, '零钱');

-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `password` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES (1, '邢天然', '123456');
INSERT INTO `tb_user` VALUES (2, '于清源', '123456');

SET FOREIGN_KEY_CHECKS = 1;
联系方式
文章目录