WinForm(C#)+SQL Server实现超市消费管理系统

一 、导言:

        借着课程设计的机会自做了一个基于RFID的管理系统,这只包含了上位机部分,完善不是很好,仅供参考。

        界面包括:登录页面,注册页面,管理员页面,用户页面等;

管理员需要注册账号登录,用户是通过管理员进行添加,管理员可以更改用户所有信息;

文章结尾有完整的源代码展示!

二 、功能结构:

        8b1b37ef8933463a8a0d77aec93bad69.png

三 、运行界面

1、登录主页面

0117d3af726949f4a230c1909e598204.png

2、注册页面

9ec1277a844a4f0cba1c743d1def43f3.png

 3、管理员页面

d2ffa4c0edca45cc9f845dd2e2422ce9.png

4、添加用户页面 

5b086220f00440fb984401be4aabc991.png

5、修改用户页面 

06feb7c763544a7aa49041c098a6ab79.png

6、修改登录密码

11f2c5faaa1d45adb299f1e33207e53a.png

7、用户页面

1fcdc99de821453e8058c30bccdea08c.png

8、消费页面

e1c1ab1feb2f4a61846f77fdd7c28c8f.png

9、充值页面

5b67d29009e7452f90a506f21fb6b0be.png

10、修改密码页面   

2f6d754da57848e1866d182748bff6f2.png

四、数据库链接 

class Class1    //生成class1类,层层调用
    {
       // SqlConnection sc1;
        public SqlConnection connection()
        {
            string str = "Data Source=DESKTOP-FI4QIMK;Initial Catalog=超市消费信息管理;Integrated Security=True";
            SqlConnection sc = new SqlConnection(str);
            sc.Open();    //打开数据库链接
            return sc;
        }
        public SqlCommand command(string sql)
        {
            SqlCommand sc = new SqlCommand(sql, connection());
            return sc;
        }

五、部分代码分析 

1、登录页面

 private void button1_Click(object sender, EventArgs e)    //  登录
        {
            if (textBox1.Text == "" || textBox2.Text == "" || comboBox1.Text == "")
            {
                MessageBox.Show("请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
            if (comboBox1.Text == "顾客")
            {
                string sql = "select *from consumer where ID ='" + textBox1.Text + "'and PassWord ='" + textBox2.Text+"'";
                Class1 class1 = new Class1();
                IDataReader mn = class1.read(sql);
                if (mn.Read())
                {
                    string gID = mn["ID"].ToString();
                    string name = mn["Name"].ToString();
                    string ye = mn["YE"].ToString();
                    string jf = mn["JF"].ToString();
                    Form3 form3 = new Form3(gID,name,ye,jf);
                    form3.Show();
                    this.Hide();
                    serialPort1.Close();

                }
                else
                {
                    MessageBox.Show("输入信息有误,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    textBox2.Text = null;
                }
            }
            if (comboBox1.Text == "管理员")
            {
                string sql = "select *from manager where ID ='" + textBox1.Text + "'and PassWord ='" + textBox2.Text+"'";
                Class1 class1 = new Class1();
                IDataReader mn = class1.read(sql);
                if (mn.Read())
                {
                    string gID1 = mn["ID"].ToString();
                    Form2 form2 = new Form2(gID1);
                    form2.Show();
                    this.Hide();
                    serialPort1.Close();
                }
                else
                {
                    MessageBox.Show("输入信息有误,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    textBox2.Text = null;
                }
            }
        }

   2、注册页面

private void button2_Click(object sender, EventArgs e)    //    注册
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
            {
                MessageBox.Show("输入信息不完整,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (textBox3.Text == textBox4.Text)
            {
                string sql = "insert into manager values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
               // MessageBox.Show(sql);      //    测试数据库语句是否正确
                Class1 class1 = new Class1();
                class1.Excute(sql);
                MessageBox.Show("注册成功");
                this.Hide();// 添加顾客信息窗口隐藏
                serialPort1.Close();

            }
            else
            {
                MessageBox.Show("两次密码输入不一样,请重新输入!");
                textBox3.Text = null;
                textBox4.Text = null;
            }
        }

   3、管理员页面

 private void toolStripButton1_Click(object sender, EventArgs e)      //    添加
        {
            Form22 form22 = new Form22();
            form22.ShowDialog();
            this.Hide();
           Form2 form2 = new Form2(gid);
            Table();
            form2.Show();  
           
        }
    

        private void toolStripButton2_Click(object sender, EventArgs e)      //    修改
        {
        string[] str = { dataGridView1.SelectedCells[0].Value.ToString(), dataGridView1.SelectedCells[1].Value.ToString(), dataGridView1.SelectedCells[2].Value.ToString(), dataGridView1.SelectedCells[3].Value.ToString() };
        //    MessageBox.Show(str[0] + str[1]); // 测试显示信息
        Form21 form21 = new Form21(str);// 通过构造方法传到窗体中去
        form21.ShowDialog();
        this.Hide();
         Table();
        Form2 form2 = new Form2(gid);
        form2.Show();
    }

        private void toolStripButton3_Click(object sender, EventArgs e)    //    删除
        {
            DialogResult r = MessageBox.Show("是否删除", "提示", MessageBoxButtons.OKCancel);
            if (r == DialogResult.OK)
            {
                string ID, Name, YE, JF;
                ID = dataGridView1.SelectedCells[0].Value.ToString();
                Name = dataGridView1.SelectedCells[1].Value.ToString();
                YE = dataGridView1.SelectedCells[2].Value.ToString();
                JF = dataGridView1.SelectedCells[3].Value.ToString();
                string sql = "delete from consumer where ID ='" + ID + "'and Name='" + Name + "'and YE = '"+YE+"'and JF = '"+JF+"'";
              //  MessageBox.Show(sql);     //      测试显示信息
                Class1 class1 = new Class1();
                class1.Excute(sql);
                this.Hide (); 
                Table();
                Form2 form2 = new Form2(gid);
                form2.ShowDialog();
            }
        }

        private void button1_Click(object sender, EventArgs e)           //   退出按键
        {
            Application.Exit();    //   结束整个系统
        }

       

        private void Form2_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
        private void toolStripButton4_Click(object sender, EventArgs e)  //  注销
        {
            DialogResult a = MessageBox.Show("是否注销!", "提示", MessageBoxButtons.OKCancel);
            if (a == DialogResult.OK)
            {
                string sql = "delete from manager where ID = '" + gid + "'";
                // MessageBox.Show(sql); 
                Class1 class2 = new Class1();
                class2.Excute(sql);
                MessageBox.Show("注销成功!");
                Application.Exit();
            }
        }

        private void toolStripButton5_Click(object sender, EventArgs e)  //   改密
        {
            Form23 form23 = new Form23(gid);
            form23.ShowDialog();
            this.Hide();
            Form2 form2 = new Form2(gid);
           // Table();
            form2.Show();
        }

 4、用户页面

private void toolStripButton4_Click(object sender, EventArgs e)   //  注销
        {
            DialogResult a = MessageBox.Show("是否注销!", "提示", MessageBoxButtons.OKCancel);
            if (a == DialogResult.OK)
            {
                string sql = "delete from consumer where ID = '" + GID1+ "'";
                // MessageBox.Show(sql); 
                Class1 class2 = new Class1();
                class2.Excute(sql);
                MessageBox.Show("注销成功!");
                Application.Exit();
            }
        }

        private void toolStripButton1_Click_1(object sender, EventArgs e) //   购物
        {
            Form31 form31 = new Form31(GID1, Name1, YE1, JF1);
            form31.ShowDialog();
            this.Hide();
        }

       

        private void toolStripButton2_Click(object sender, EventArgs e)   //  充值
        {
            Form32 form32 = new Form32(GID1, Name1, YE1, JF1);
            form32.ShowDialog();
            this.Hide();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)    //   改密
        {
            Form33 f = new Form33(GID1);
            f.ShowDialog();
        }

由于内容较多就不全部展示了,全部内容放在下方的源代码中

六、源代码展示 

本文展示部分的代码,完整代码在下方的百度网盘里,有需要的可以自行提取:

链接:https://pan.baidu.com/s/1LC-qW2wTvBtYzRFVnEz1qQ?pwd=ojbk 
提取码:ojbk

有问题可以联系我! 

欢迎指教!

  • 6
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,我会尽可能地详细回答你的问题。首先,我们需要了解一下WinFormSQL Server的基本概念。 WinFormC#编程语言中的一个GUI(图形用户界面)框架,它是Microsoft .NET平台的一部分。它提供了一些控件和组件,使我们能够轻松地创建Windows应用程序。而SQL Server则是一种关系型数据库管理系统,也是由Microsoft开发的。 接下来,我们需要了解一下如何使用C#SQL Server实现聊天工具。 1. 创建数据库 首先,我们需要创建一个名为“Chat”的数据库。我们可以使用SQL Server Management Studio来创建它。在创建数据库之后,我们需要创建两个表:一个是“Users”表,一个是“Messages”表。 “Users”表中包含用户的ID、用户名和密码等信息;“Messages”表中包含消息的ID、发送者ID、接收者ID、消息内容、消息类型和发送时间等信息。 2. 连接数据库 在C#中,我们可以使用SqlConnection类来连接SQL Server数据库。我们需要使用连接字符串来指定数据库的位置和登录信息。 ```csharp SqlConnection connection = new SqlConnection("Data Source=server;Initial Catalog=Chat;User ID=username;Password=password"); connection.Open(); ``` 3. 实现登录功能 我们可以使用WinForm中的TextBox控件来让用户输入用户名和密码。在用户点击“登录”按钮后,我们需要检查用户名和密码是否正确。如果正确,我们可以在“Users”表中查询用户的ID,并将其保存在全局变量中。 ```csharp SqlCommand command = new SqlCommand("SELECT ID FROM Users WHERE Username=@username AND Password=@password", connection); command.Parameters.AddWithValue("@username", username); command.Parameters.AddWithValue("@password", password); int userID = (int)command.ExecuteScalar(); if(userID != null) { // 登录成功,保存用户ID currentUserID = userID; } else { // 登录失败,提示用户 MessageBox.Show("用户名或密码错误!"); } ``` 4. 实现发送消息功能 我们可以使用WinForm中的RichTextBox控件来让用户输入消息内容。在用户点击“发送”按钮后,我们需要将消息保存到“Messages”表中,并将消息发送给接收者。 ```csharp // 获取消息内容 string message = richTextBoxMessage.Text; // 保存消息到数据库中 SqlCommand command = new SqlCommand("INSERT INTO Messages (SenderID, ReceiverID, MessageContent, MessageType, SendTime) VALUES (@senderID, @receiverID, @messageContent, @messageType, GETDATE())", connection); command.Parameters.AddWithValue("@senderID", currentUserID); command.Parameters.AddWithValue("@receiverID", receiverID); command.Parameters.AddWithValue("@messageContent", message); command.Parameters.AddWithValue("@messageType", "text"); command.ExecuteNonQuery(); // 发送消息给接收者 client.Send(message); ``` 5. 实现发送文件功能 如果用户想要发送文件,我们可以使用WinForm中的OpenFileDialog控件来让用户选择文件。然后,我们需要将文件的二进制数据保存到“Messages”表中,并将消息发送给接收者。 ```csharp // 让用户选择文件 OpenFileDialog dialog = new OpenFileDialog(); if(dialog.ShowDialog() == DialogResult.OK) { // 读取文件的二进制数据 byte[] fileData = File.ReadAllBytes(dialog.FileName); // 保存文件到数据库中 SqlCommand command = new SqlCommand("INSERT INTO Messages (SenderID, ReceiverID, MessageContent, MessageType, SendTime) VALUES (@senderID, @receiverID, @messageContent, @messageType, GETDATE())", connection); command.Parameters.AddWithValue("@senderID", currentUserID); command.Parameters.AddWithValue("@receiverID", receiverID); command.Parameters.AddWithValue("@messageContent", fileData); command.Parameters.AddWithValue("@messageType", "file"); command.ExecuteNonQuery(); // 发送消息给接收者 client.SendFile(dialog.FileName); } ``` 6. 实现发送表情包功能 如果用户想要发送表情包,我们可以使用WinForm中的PictureBox控件来让用户选择表情包。然后,我们需要将表情包的二进制数据保存到“Messages”表中,并将消息发送给接收者。 ```csharp // 让用户选择表情包 OpenFileDialog dialog = new OpenFileDialog(); if(dialog.ShowDialog() == DialogResult.OK) { // 读取表情包的二进制数据 byte[] emojiData = File.ReadAllBytes(dialog.FileName); // 保存表情包到数据库中 SqlCommand command = new SqlCommand("INSERT INTO Messages (SenderID, ReceiverID, MessageContent, MessageType, SendTime) VALUES (@senderID, @receiverID, @messageContent, @messageType, GETDATE())", connection); command.Parameters.AddWithValue("@senderID", currentUserID); command.Parameters.AddWithValue("@receiverID", receiverID); command.Parameters.AddWithValue("@messageContent", emojiData); command.Parameters.AddWithValue("@messageType", "emoji"); command.ExecuteNonQuery(); // 发送消息给接收者 client.SendEmoji(dialog.FileName); } ``` 7. 实现下载文件功能 如果用户想要下载文件,我们可以使用WinForm中的SaveFileDialog控件来让用户选择文件保存位置。然后,我们需要从“Messages”表中读取文件的二进制数据,并将其保存到用户选择的位置。 ```csharp // 让用户选择文件保存位置 SaveFileDialog dialog = new SaveFileDialog(); if(dialog.ShowDialog() == DialogResult.OK) { // 从数据库中读取文件的二进制数据 SqlCommand command = new SqlCommand("SELECT MessageContent FROM Messages WHERE ID=@messageID", connection); command.Parameters.AddWithValue("@messageID", messageID); byte[] fileData = (byte[])command.ExecuteScalar(); // 保存文件到用户选择的位置 File.WriteAllBytes(dialog.FileName, fileData); } ``` 8. 实现查询历史聊天记录功能 如果用户想要查询历史聊天记录,我们可以使用WinForm中的DataGridView控件来显示记录。我们需要从“Messages”表中读取所有与当前用户有关的记录,并将它们显示在DataGridView中。 ```csharp // 从数据库中读取所有与当前用户有关的记录 SqlCommand command = new SqlCommand("SELECT * FROM Messages WHERE SenderID=@currentUserID OR ReceiverID=@currentUserID", connection); command.Parameters.AddWithValue("@currentUserID", currentUserID); SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable table = new DataTable(); adapter.Fill(table); // 将记录显示在DataGridView中 dataGridViewMessages.DataSource = table; ``` 以上是WinForm+SQL Server聊天工具的基本实现过程和代码示例。当然,这只是一个简单的示例,实际应用中还需要处理很多细节问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有你真好...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值