您的当前位置:首页正文

用winform程序简单的开发一个超市管理系统,实现增删改查

2024-11-22 来源:个人技术集锦

主页

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 AdvancedFiltration
{
    public partial class manageSystem : Form
    {
        Form Head;//头部的子窗体
        public manageSystem()
        {
            InitializeComponent();
        }

        private void ManageSystem_Load(object sender, EventArgs e)
        {
            
        }

        private void Label1_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 进入用户管理页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnUser_Click(object sender, EventArgs e)
        {
            this.Visible = false;//该窗口隐藏
            userManage um = new userManage();//实例化用户管理窗口
            um.Show();//显示用户管理窗口
        }
        /// <summary>
        /// 进入用户管理页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCustomer_Click(object sender, EventArgs e)
        {
            this.Visible = false;//该窗口隐藏
            customerManage cm = new customerManage();//实例化客户管理窗口
            cm.Show();//显示用户管理窗口
        }
        /// <summary>
        /// 进入订单管理页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnOrder_Click(object sender, EventArgs e)
        {
            this.Visible = false;//该窗口隐藏
            orderManage om = new orderManage();//实例化客户管理窗口
            om.Show();//显示用户管理窗口
        }
        /// <summary>
        /// 退出系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();//关闭整个系统
        }
    }
}

1.用户管理界面

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 AdvancedFiltration
{
    public partial class userManage : Form
    {
        public userManage()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 根据用户名称,查询数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnUserFind_Click(object sender, EventArgs e)
        {
            string strSql = string.Format("select * from userManage where userName = '{0}'", txtUserName.Text);
            AdoHelp ah = new AdoHelp();//实例化SQL操作辅助类
            DataSet ds = ah.ExecuteQueryDataSet(strSql);//执行查询语句,返回DataSet集合
            dataGridView1.DataSource = ds.Tables[0];
        }
        /// <summary>
        ///在DataGridView中修改,并更新到数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string strColumn = dataGridView1.Columns[e.ColumnIndex].HeaderText;//获取列标题
            string strRow = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();//获取焦点触发行的第一个值,一般来说,都是表的主键
            string ColumnValue = dataGridView1.CurrentCell.Value.ToString();//获取当前点击的活动单元格的值
            //定义SQL语言
            string strSql = string.Format("update userManage set {0} = '{1}' where userId = {2}", strColumn, ColumnValue, strRow);
            AdoHelp ah = new AdoHelp();//实例化SQL增删改查辅助类对象
            int result = ah.ExecuteNonQuery(strSql);//增删改操作,返回受影响的值
            if (result > 0)
            {
                MessageBox.Show("修改成功!");
            }
            else
            {
                MessageBox.Show("修改失败!");
            }
        }
        /// <summary>
        /// 返回首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label2_Click(object sender, EventArgs e)
        {
            this.Visible = false;//隐藏本页面(用户管理)
            manageSystem ms = new manageSystem();//实例化管理系统页面
            ms.Show();//显示管理系统界面
        }
        //进入添加数据页面
        private void BtnAddTo_Click(object sender, EventArgs e)
        {
            this.Visible = false;//隐藏当前页面(用户管理页面)
            AddUserForm auf = new AddUserForm();//实例化添加用户窗口
            auf.Show();//显示此窗口
        }
        /// <summary>
        /// 删除选中的单元行数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnUserDelete_Click(object sender, EventArgs e)
        {
            //dataGridView1.AllowUserToAddRows = false;//删除最后一行的空白行
            //dataGridView1.Rows.Remove(dataGridView1.CurrentRow);//删除当前光标所在行 
            //dataGridView1.Rows.Clear();//删除所有行
            
            
            string value = this.dataGridView1.SelectedCells[0].Value.ToString();//获取选中单元格的值
            //获取所选中的单元格的列名
            string column = this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].HeaderText;
            DialogResult dr = MessageBox.Show("是否删除此条数据", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
            if(dr == DialogResult.OK)
            {
                //定义SQL删除指令
                string strSql = string.Format("delete from userManage where {0} = '{1}'", column, value);
                AdoHelp ah = new AdoHelp();//实例化SQL增删改查辅助类对象
                int result = ah.ExecuteNonQuery(strSql);//返回受影响的行数
                if (result > 0)
                {
                    MessageBox.Show("删除成功");
                    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);//删除当前光标所在行 
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            else
            {
                MessageBox.Show("已取消操作");
            }
            
            
        }
        /// <summary>
        /// 查询用户管理表的全部数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSelectUserAll_Click(object sender, EventArgs e)
        {
            string strSql = "select * from userManage";
            AdoHelp ah = new AdoHelp();//实例化ADO.net类
            DataSet ds =  ah.ExecuteQueryDataSet(strSql);
            dataGridView1.DataSource = ds.Tables[0];
            //实例化DataSet集合输出到Excel对象
            dataSetToExcel dte = new dataSetToExcel();
            dte.DataSetToExcel(ds, true);//输出到Excel
        }
    }
}

1.1添加用户页面

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AdvancedFiltration
{
    public partial class AddUserForm : Form
    {
        public AddUserForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 添加用户数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAddUser_Click(object sender, EventArgs e)
        {
            string userId = txtUserId.Text;//获取编号
            string userName = txtUserName.Text;//获取名称
            string userSex = txtUserSex.Text;//获取性别
            string userAge = txtUserAge.Text;//获取年龄
            string userPhone = txtUserPhone.Text;//获取电话
            string userAddress = txtUserAddress.Text;//获取地址
            string userJurisdiction = txtUserJurisdiction.Text;//获取权限
            //MessageBox.Show(userId + "-" + userName + "-" + userSex + "-" + userAge + "-" + userPhone + "-" + userAddress + "-" + userJurisdiction);
            //编写插入SQL语句
            string strSql = "insert into userManage values (@userId, @userName, @userSex, @userAge, @userPhone, @userAddress, @userJurisdiction)";
                                                            //注意:@userName...这里都不要加引号,如果用的是string.format才需要
            //创建输入参数数组
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("@userId", userId),
                new SqlParameter("@userName", userName),
                new SqlParameter("@userSex", userSex),
                new SqlParameter("@userAge", userAge),
                new SqlParameter("@userPhone", userPhone),
                new SqlParameter("@userAddress", userAddress),
                new SqlParameter("@userJurisdiction", userJurisdiction),
            };
            AdoHelp ah = new AdoHelp();//实例化sql增删改查辅助类
            int result = ah.ExecuteNonQuery(strSql, paras);//返回受影响的函数
            if(result > 0)
            {
                MessageBox.Show("成功添加数据");
                //清空数据
                txtUserId.Text = "";//获取编号
                txtUserName.Text = "";//获取名称
                txtUserSex.Text = "";//获取性别
                txtUserAge.Text = "";//获取年龄
                txtUserPhone.Text = "";//获取电话
                txtUserAddress.Text = "";//获取地址
                txtUserJurisdiction.Text = "";//获取权限
            }
            else
            {
                MessageBox.Show("添加数据失败");
            }
        }
        //前往首页
        private void Label8_Click(object sender, EventArgs e)
        {
            this.Visible = false;//隐藏当前页面
            manageSystem ms = new manageSystem();//实例化管理系统窗口
            ms.Show();//显示窗口
        }
        //回到用户管理页面
        private void Label9_Click(object sender, EventArgs e)
        {
            this.Visible = false;//隐藏当前页面
            userManage um = new userManage();//实例化用户管理页面
            um.Show();//显示页面
        }
    }
}

看完这一个用户管理就可以了,后面的代码都差不多

2.客户管理界面

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 AdvancedFiltration
{
    public partial class customerManage : Form
    {
        public customerManage()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 根据客户名称,查询数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSelectCustomerName_Click(object sender, EventArgs e)
        {
            string strSql = string.Format("select * from customerManage where customerName = '{0}'", txtCustomerName.Text);
            AdoHelp ah = new AdoHelp();//实例化SQL操作辅助类
            DataSet ds = ah.ExecuteQueryDataSet(strSql);//执行查询语句,返回DataSet集合
            dataGridView1.DataSource = ds.Tables[0];
        }
        /// <summary>
        /// 进入添加数据页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAddCustomer_Click(object sender, EventArgs e)
        {
            this.Visible = false;//隐藏当前页面(用户管理页面)
            AddCustomer ac = new AddCustomer();//实例化添加客户窗口
            ac.Show();//显示此窗口
        }
        /// <summary>
        /// 在dataGridView里修改数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string strColumn = dataGridView1.Columns[e.ColumnIndex].HeaderText;//获取列标题
            string strRow = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();//获取焦点触发行的第一个值,一般来说,都是表的主键
            string ColumnValue = dataGridView1.CurrentCell.Value.ToString();//获取当前点击的活动单元格的值
            //定义SQL语言
            string strSql = string.Format("update customerManage set {0} = '{1}' where customerId = {2}", strColumn, ColumnValue, strRow);
            AdoHelp ah = new AdoHelp();//实例化SQL增删改查辅助类对象
            int result = ah.ExecuteNonQuery(strSql);//增删改操作,返回受影响的值
            if (result > 0)
            {
                MessageBox.Show("修改成功!");
            }
            else
            {
                MessageBox.Show("修改失败!");
            }
        }
        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnDeleteCustomer_Click(object sender, EventArgs e)
        {
            string value = this.dataGridView1.SelectedCells[0].Value.ToString();//获取选中单元格的值
            //获取所选中的单元格的列名
            string column = this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].HeaderText;
            DialogResult dr = MessageBox.Show("是否删除此条数据", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
            if (dr == DialogResult.OK)
            {
                //定义SQL删除指令
                string strSql = string.Format("delete from customerManage where {0} = '{1}'", column, value);
                AdoHelp ah = new AdoHelp();//实例化SQL增删改查辅助类对象
                int result = ah.ExecuteNonQuery(strSql);//返回受影响的行数
                if (result > 0)
                {
                    MessageBox.Show("删除成功");
                    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);//删除当前光标所在行 
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            else
            {
                MessageBox.Show("已取消操作");
            }
        }
        /// <summary>
        /// 查询全部客户数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSelectAllCustomer_Click(object sender, EventArgs e)
        {
            string strSql = "select * from customerManage";
            AdoHelp ah = new AdoHelp();//实例化ADO.net类
            DataSet ds = ah.ExecuteQueryDataSet(strSql);
            dataGridView1.DataSource = ds.Tables[0];
            //实例化DataSet集合输出到Excel对象
            dataSetToExcel dte = new dataSetToExcel();
            dte.DataSetToExcel(ds, true);//输出到Excel
        }
        /// <summary>
        /// 返回首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label1_Click(object sender, EventArgs e)
        {
            this.Visible = false;//隐藏本页面(客户管理)
            manageSystem ms = new manageSystem();//实例化管理系统页面
            ms.Show();//显示管理系统界面
        }

        private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

2.2添加客户

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AdvancedFiltration
{
    public partial class AddUserForm : Form
    {
        public AddUserForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 添加用户数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAddUser_Click(object sender, EventArgs e)
        {
            string userId = txtUserId.Text;//获取编号
            string userName = txtUserName.Text;//获取名称
            string userSex = txtUserSex.Text;//获取性别
            string userAge = txtUserAge.Text;//获取年龄
            string userPhone = txtUserPhone.Text;//获取电话
            string userAddress = txtUserAddress.Text;//获取地址
            string userJurisdiction = txtUserJurisdiction.Text;//获取权限
            //MessageBox.Show(userId + "-" + userName + "-" + userSex + "-" + userAge + "-" + userPhone + "-" + userAddress + "-" + userJurisdiction);
            //编写插入SQL语句
            string strSql = "insert into userManage values (@userId, @userName, @userSex, @userAge, @userPhone, @userAddress, @userJurisdiction)";
                                                            //注意:@userName...这里都不要加引号,如果用的是string.format才需要
            //创建输入参数数组
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("@userId", userId),
                new SqlParameter("@userName", userName),
                new SqlParameter("@userSex", userSex),
                new SqlParameter("@userAge", userAge),
                new SqlParameter("@userPhone", userPhone),
                new SqlParameter("@userAddress", userAddress),
                new SqlParameter("@userJurisdiction", userJurisdiction),
            };
            AdoHelp ah = new AdoHelp();//实例化sql增删改查辅助类
            int result = ah.ExecuteNonQuery(strSql, paras);//返回受影响的函数
            if(result > 0)
            {
                MessageBox.Show("成功添加数据");
                //清空数据
                txtUserId.Text = "";//获取编号
                txtUserName.Text = "";//获取名称
                txtUserSex.Text = "";//获取性别
                txtUserAge.Text = "";//获取年龄
                txtUserPhone.Text = "";//获取电话
                txtUserAddress.Text = "";//获取地址
                txtUserJurisdiction.Text = "";//获取权限
            }
            else
            {
                MessageBox.Show("添加数据失败");
            }
        }
        //前往首页
        private void Label8_Click(object sender, EventArgs e)
        {
            this.Visible = false;//隐藏当前页面
            manageSystem ms = new manageSystem();//实例化管理系统窗口
            ms.Show();//显示窗口
        }
        //回到用户管理页面
        private void Label9_Click(object sender, EventArgs e)
        {
            this.Visible = false;//隐藏当前页面
            userManage um = new userManage();//实例化用户管理页面
            um.Show();//显示页面
        }
    }
}

3.订单管理

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 AdvancedFiltration
{
    public partial class orderManage : Form
    {
        public orderManage()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 返回首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label1_Click(object sender, EventArgs e)
        {
            this.Visible = false;//隐藏本页面(订单管理)
            manageSystem ms = new manageSystem();//实例化管理系统页面
            ms.Show();//显示管理系统界面
        }
        /// <summary>
        /// 根据订单名称,查询订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSelectOrderName_Click(object sender, EventArgs e)
        {
            string strSql = string.Format("select * from orderManage where orderName = '{0}'", txtOrderName.Text);
            AdoHelp ah = new AdoHelp();//实例化SQL操作辅助类
            DataSet ds = ah.ExecuteQueryDataSet(strSql);//执行查询语句,返回DataSet集合
            dataGridView1.DataSource = ds.Tables[0];
        }
        /// <summary>
        /// 去添加数据页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnToAddOrder_Click(object sender, EventArgs e)
        {
            this.Visible = false;//隐藏当前页面(订单管理页面)
            AddOrder ao = new AddOrder();//实例化添加订单窗口
            ao.Show();//显示此窗口
        }
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnDeleteOrder_Click(object sender, EventArgs e)
        {
            string value = this.dataGridView1.SelectedCells[0].Value.ToString();//获取选中单元格的值
            //获取所选中的单元格的列名
            string column = this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].HeaderText;
            DialogResult dr = MessageBox.Show("是否删除此条数据", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
            if (dr == DialogResult.OK)
            {
                //定义SQL删除指令
                string strSql = string.Format("delete from orderManage where {0} = '{1}'", column, value);
                AdoHelp ah = new AdoHelp();//实例化SQL增删改查辅助类对象
                int result = ah.ExecuteNonQuery(strSql);//返回受影响的行数
                if (result > 0)
                {
                    MessageBox.Show("删除成功");
                    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);//删除当前光标所在行 
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            else
            {
                MessageBox.Show("已取消操作");
            }
        }
        /// <summary>
        /// 查询所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSelectAllOrder_Click(object sender, EventArgs e)
        {
            string strSql = "select * from orderManage";
            AdoHelp ah = new AdoHelp();//实例化ADO.net类
            DataSet ds = ah.ExecuteQueryDataSet(strSql);
            dataGridView1.DataSource = ds.Tables[0];
            //实例化DataSet集合输出到Excel对象
            dataSetToExcel dte = new dataSetToExcel();
            dte.DataSetToExcel(ds, true);//输出到Excel
        }
        /// <summary>
        /// 单元格修改,修改数据库数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string strColumn = dataGridView1.Columns[e.ColumnIndex].HeaderText;//获取列标题
            string strRow = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();//获取焦点触发行的第一个值,一般来说,都是表的主键
            string ColumnValue = dataGridView1.CurrentCell.Value.ToString();//获取当前点击的活动单元格的值
            //定义SQL语言
            string strSql = string.Format("update orderManage set {0} = '{1}' where orderId = {2}", strColumn, ColumnValue, strRow);
            AdoHelp ah = new AdoHelp();//实例化SQL增删改查辅助类对象
            int result = ah.ExecuteNonQuery(strSql);//增删改操作,返回受影响的值
            if (result > 0)
            {
                MessageBox.Show("修改成功!");
            }
            else
            {
                MessageBox.Show("修改失败!");
            }
        }

        private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void OrderManage_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 把订单管理表插入到oMana表中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label3_Click(object sender, EventArgs e)
        {
            string strSql = "EXEC ser";//执行存储过程ser
            AdoHelp ah = new AdoHelp();//实例化ADO.NET类
            int result =  ah.ExecuteNonQuery(strSql);
            if(result > 0)
            {
                MessageBox.Show("插入成功");
                string str = "select * from oMana";
                DataSet ds =  ah.ExecuteQueryDataSet(str);//查询oMana表
                dataGridView1.DataSource = ds.Tables[0];
            }
            else
            {
                MessageBox.Show("插入失败");
            }
        }
    }
}

显示全文