美文网首页
智慧商超登录系统

智慧商超登录系统

作者: a9ee6531b2a6 | 来源:发表于2018-12-04 18:12 被阅读0次

    项目简介

    智慧社区是社区管理的一种新理念,是新形势下社会管理创新的一种新模式。
    智慧社区是指充分利用物联网、云计算、移动互联网等新一代信息技术的集成 应用,为社区居民提供一个安全、舒适、便利的现代化、智慧化生活环境,从而 形成基于信息化、智能化社会管理与服务的一种新的管理形态的社区。
    智慧社区服务的内容一般包括:智慧物业管理、电子商务服务、智慧养老服 务、智慧家居等。

    制作成功的登录界面

    image.png

    其对应代码

    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;
    using System.Data.SqlClient;
    using System.Configuration;
    
    namespace WindowsFormsApplication2
    {
        public partial class LoginForm1 : Form
        {
            public LoginForm1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                this.comboBox1.SelectedIndex = 0;
            }
    
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    #region
                /*if (this.textBox2.Text == "" || this.textBox1.Text == "")
                {
                    MessageBox.Show("用户名密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (this.comboBox1.SelectedItem.ToString() == "收银员")
                    {
                        if (this.textBox2.Text == "mqj" && this.textBox1.Text == "2017270399"
                            || this.textBox2.Text == "1" && this.textBox1.Text == "1")
                        {
                            MessageBox.Show("收银员登录成功");
                           
    
                            this.Hide();
    
                            Form2 fm2 = new Form2();
                            fm2.usename = this.textBox2.Text.ToString();
                            //fm2.MdiParent = this;
                            fm2.Show();
                        }
                        else
                        {
                            MessageBox.Show("用户名或者密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
    
                        }
                    }
                    if (this.comboBox1.SelectedItem.ToString() == "库管员")
                    {
                        if (this.textBox2.Text == "2" && this.textBox1.Text == "2"
                            || this.textBox2.Text == "lp" && this.textBox1.Text == "2017270370")
                        {
                            MessageBox.Show("库管员登录成功");
    
                            this.Hide();
    
                            Form3 fm3 = new Form3();
                            fm3.usename = this.textBox2.Text.ToString();
                            //fm2.MdiParent = this;
                            fm3.Show();
                        }
                        else
                        {
                            MessageBox.Show("用户名或者密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
    
                        }
                    }
    
                }*/
    #endregion      
                //string connStr = "Data Source=.;Initial Catalog=cxy2017270402;Integrated Security=True";
                string connStr = ConfigurationManager.ConnectionStrings["cxy2017270402"].ConnectionString;
                SqlConnection sqlConn = new SqlConnection(connStr);
                try
                {
                    //连接数据库
                    sqlConn.Open();
                    String sqlStr = "select * from 雇员 where ID=@id and PASSWORD=@pwd";
                    SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
    
                    //注意用用户ID登录,而不是用户名,用户民可能会重复
                    cmd.Parameters.Add(new SqlParameter("@id", this.textBox2.Text.Trim()));
                    cmd.Parameters.Add(new SqlParameter("@pwd", this.textBox1.Text.Trim()));
    
                    SqlDataReader dr = cmd.ExecuteReader();
    
                    if (dr.HasRows)
                    {
                        dr.Read();
                        UserInfo.userId = int.Parse(dr["ID"].ToString());
                        UserInfo.userName = dr["NAME"].ToString();
                        UserInfo.userPwd = dr["PASSWORD"].ToString();
                        UserInfo.userPhone = dr["PHONE"].ToString();
                        UserInfo.userType = dr["TYPE"].ToString();
                        
    
                        MessageBox.Show(UserInfo.userType + "登录成功");
                        if (UserInfo.userType == "收银员")
                        {
                            //显示收银员主界面
                            UserForm2 formUser = new UserForm2();
                            formUser.Show();
    
                            //隐藏登陆界面
                            this.Hide();
                        }
    
                        if (UserInfo.userType == "库管员")
                        {
                            //显示库管员主界面
                            AdminForm3 formAdmin = new AdminForm3();
                            formAdmin.Show();
    
                            //隐藏登陆界面
                            this.Hide();
                        }
                    }
                    else
                    {
                        MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    #region
                    /*if (this.comboBox1.Text == "收银员")
                    {
                        sqlStr = "select * from 收银员 where ID=@id and PASSWORD=@pwd";
                    }
                    else
                    {
                        sqlStr = "select * from 库管员 where ID=@id and PASSWORD=@pwd";
                    }
                    SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
    
                    //注意用用户ID登录,而不是用户名,用户民可能会重复
                    cmd.Parameters.Add(new SqlParameter("@id", this.textBox2.Text.Trim()));
                    cmd.Parameters.Add(new SqlParameter("@pwd", this.textBox1.Text.Trim()));
    
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        dr.Read();
                        String UserId = dr["ID"].ToString();
                        String UserName = dr["NAME"].ToString();
                        String UserPwd = dr["PASSWORD"].ToString();
                        String UserPhone = dr["PHONE"].ToString();
                        String UserType = this.comboBox1.Text;
    
                        MessageBox.Show(UserType + "登录成功");
                            if (UserType == "收银员")
                        {
                            //显示收银员主界面
                            Form2 formUser = new Form2();
                            formUser.Show();
    
                            //隐藏登陆界面
                            this.Hide();
                        }
    
                        if (UserType == "库管员")
                        {
                            //显示库管员主界面
                            Form3 formAdmin = new Form3();
                            formAdmin.Show();
    
                            //隐藏登陆界面
                            this.Hide();
                        }
                    
                    }*/
                  #endregion  
                }
                    catch (Exception exp)
                    {
                        MessageBox.Show("数据库访问出错");
                    }
                    finally
                    {
                        sqlConn.Close();
                    }
                
            }
    
            private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
            {
                MessageBox.Show("请联系管理员", "提示", MessageBoxButtons.OK);
            }
    
    
            private void textBox2_Enter(object sender, EventArgs e)
            {
                ((TextBox)sender).SelectAll();
            }
    
            private void textBox1_Enter(object sender, EventArgs e)
            {
                ((TextBox)sender).SelectAll();
            }
    
            private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)Keys.Enter)
                {
                    SendKeys.Send("{tab}");
                }
    
    
            }
    
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)Keys.Enter)
                {
                    this.button1_Click(sender, e);
                }
            }
        }
    }
    
    

    对应代码

    收银员界面

    image.png
    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 WindowsFormsApplication2
    {
        public partial class UserForm2 : Form
        {
            public string usename;
    
            public UserForm2()
            {
                InitializeComponent();
            }
            //修改密码
            private void toolStripMenuItem1_Click(object sender, EventArgs e)
            {
                PwdForm4 pwdForm = new PwdForm4();
                pwdForm.MdiParent = this;
                pwdForm.StartPosition = FormStartPosition.CenterScreen;
                pwdForm.Show();
            }
    
            private void toolStripStatusLabel1_Click(object sender, EventArgs e)
            {
    
            }
            //修改用户信息
            private void toolStripMenuItem2_Click(object sender, EventArgs e)
            {
    
            }
            //查询商品信息
            private void toolStripMenuItem3_Click(object sender, EventArgs e)
            {
                chaxun Goods = new chaxun();
                Goods.MdiParent = this;
                Goods.WindowState = FormWindowState.Maximized;
                Goods.Show();
            }
            //窗体加载
            private void Form2_Load(object sender, EventArgs e)
            {
                //this.toolStripStatusLabel4.Text =
                //    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                this.tssl_user.Text = this.usename;
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                //MessageBox.Show(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                this.tssl_time.Text =
                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                this.statusStrip1.Refresh();
            }
    
            private void toolStripMenuItem4_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("确认注销", "提示", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Question) == DialogResult.OK)
                {
                    //重新启动程序
                    Application.Restart();
                }
            }
    
            private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
            private void toolStripButton1_Click(object sender, EventArgs e)
            {
                this.toolStripMenuItem1_Click(sender, e);
            }
            private void toolStripButton4_Click(object sender, EventArgs e)
            {
                this.toolStripMenuItem4_Click(sender, e);
            }
            private void toolStripButton2_Click(object sender, EventArgs e)
            {
                this.toolStripMenuItem2_Click(sender, e);
            }
            private void toolStripButton3_Click(object sender, EventArgs e)
            {
                this.toolStripMenuItem3_Click(sender, e);
            }
            private void toolStripButton5_Click(object sender, EventArgs e)
            {
                this.退出ToolStripMenuItem_Click(sender, e);
            }
            private void Form2_FormClosing(object sender, FormClosingEventArgs e)
            {
                Application.Exit();
            }
            private void tssl_time_Click(object sender, EventArgs e)
            {
            }
        }
    }
    

    库管员界面

    image.png

    对应代码

    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 WindowsFormsApplication2
    {
        public partial class AdminForm3 : Form
        {
            public string usename;
            public AdminForm3()
            {
                InitializeComponent();
            }
            private void 注销LToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("确认注销", "提示", MessageBoxButtons.OKCancel,
                   MessageBoxIcon.Question) == DialogResult.OK)
                {
                    //重新启动程序
                    Application.Restart();
                }
            }
            private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
            private void toolStripButton1_Click(object sender, EventArgs e)
            {
                this.修改密码PToolStripMenuItem_Click(sender, e);
            }
            private void 修改密码PToolStripMenuItem_Click(object sender, EventArgs e)
            {
                PwdForm4 pwdForm = new PwdForm4();
                pwdForm.MdiParent = this;
                pwdForm.StartPosition = FormStartPosition.CenterScreen;
                pwdForm.Show();
            }
            private void 修改个人信息IToolStripMenuItem_Click(object sender, EventArgs e)
            {
            }
            private void toolStripButton2_Click(object sender, EventArgs e)
            {
                this.修改个人信息IToolStripMenuItem_Click(sender, e);
            }
            private void 录入商品信息SToolStripMenuItem_Click(object sender, EventArgs e)
            {   
            }
            //录入商品信息
            private void toolStripButton3_Click(object sender, EventArgs e)
            {   
            }
            //注销
            private void toolStripButton4_Click(object sender, EventArgs e)
            {
                this.注销LToolStripMenuItem_Click(sender, e);
            }
            //退出
            private void toolStripButton5_Click(object sender, EventArgs e)
            {
                this.退出EToolStripMenuItem_Click(sender, e);
            }
            private void Form3_Load(object sender, EventArgs e)
            {
                this.tssl_time.Text =
                  DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                this.tssl_user.Text = this.usename;
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                this.tssl_time.Text = 
                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                this.statusStrip1.Refresh();
            }
            private void Form3_FormClosing(object sender, FormClosingEventArgs e)
            {
                Application.Exit();
            }
            private void 录入商品信息SToolStripMenuItem_Click_1(object sender, EventArgs e)
            {
                GoodsForm Goods = new GoodsForm();
                Goods.MdiParent = this;
                Goods.Show();
            }
        }
    }
    

    修改密码界面

    image.png
    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;
    using System.Data.SqlClient;
    using System.Configuration;
    
    namespace WindowsFormsApplication2
    {
        public partial class PwdForm4 : Form
        {
            public PwdForm4()
            {
                InitializeComponent();
            }
    
            private void Form4_Load(object sender, EventArgs e)
            {
                this.textBox1.Text = UserInfo.userName;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                String userName = this.textBox1.Text.Trim();
                String newPwd = this.textBox2.Text.Trim();
                String confPwd = this.textBox3.Text.Trim();
    
                //验证输入信息
                if(newPwd.Equals(""))
                {
                    MessageBox.Show("请输入密码","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    return;
                }
                else if (confPwd.Equals(""))
                {
                    MessageBox.Show("请输入确认密码", "提示", MessageBoxButtons.OK, MessageBoxIcon. Warning);
                    return;
                }
                else if (newPwd.Equals(""))
                {
                    MessageBox.Show("两次密码不一致","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    return;
                }
                string connStr = ConfigurationManager.ConnectionStrings["cxy2017270402"].ConnectionString;
                SqlConnection sqlConn = new SqlConnection(connStr);
                try
                {
                    //连接数据库
                    sqlConn.Open();
                    
                    //构造命令
                    String sqlStr = "update 雇员 set PASSWORD=@pwd where ID=@id";
                    SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
                    
                    // SQL字符串参数赋值
                    cmd.Parameters.Add(new SqlParameter("@pwd", newPwd));
                    cmd.Parameters.Add(new SqlParameter("@id", UserInfo.userId));
    
                    // 将命令发送给数据库
                    int res = cmd.ExecuteNonQuery();
    
                    // 根据返回值判断是否修改成功
                    if (res != 0)
                    {
                        MessageBox.Show("密码修改成功");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("密码修改错误");
                    }
                }
                catch (Exception exp)
                {
                    MessageBox.Show("访问数据库错误:" + exp.ToString());
                }
                finally
                {
                    sqlConn.Close();
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                
            }
        }
    }
    

    录入商品信息

    image.png
    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;
    using System.Data.SqlClient;
    using System.Configuration;
    
    namespace WindowsFormsApplication2
    {
        public partial class GoodsForm : Form
        {
            public GoodsForm()
            {
                InitializeComponent();
            }
            private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                String id = this.textBox1.Text.Trim();
                String name = this.textBox2.Text.Trim(); 
                String price = this.textBox3.Text.Trim();
                String spec = this.textBox4.Text.Trim();
                String remark = this.textBox5.Text.Trim();
                // 连接字符串,注意与实际环境保持一致 
                String connStr = ConfigurationManager.ConnectionStrings["cxy2017270402"].ConnectionString; 
                SqlConnection sqlConn = new SqlConnection(connStr);
                try
                {
                    //连接数据库
                    sqlConn.Open();
                    //构造命令
                    String sqlStr = "insert into 商品(ID,NAME,PRICE,SPEC,REMARK) values(@id,@name,@price,@spec,@remark)";
                    SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
                    //SQL字符串参数赋值
                    cmd.Parameters.Add(new SqlParameter("@id", id));
                    cmd.Parameters.Add(new SqlParameter("@name", name));
                    cmd.Parameters.Add(new SqlParameter("@price", price));
                    cmd.Parameters.Add(new SqlParameter("@spec", spec));
                    cmd.Parameters.Add(new SqlParameter("@remark", remark));
                    //将命令发送给数据库
                    int res = cmd.ExecuteNonQuery();
                    //根据返回值判断是否插入成功
                    if (res != 0)
                    {
                        MessageBox.Show("商品录入成功");
                    }
                    else
                    {
                        MessageBox.Show("商品录入失败");
                    }
                }
                catch (Exception exp)
                {
                    MessageBox.Show("访问数据库错误:" + exp.Message);
                }
                finally
                {
                    sqlConn.Close();
                }
            }
            private void textBox3_TextChanged(object sender, EventArgs e)
            {
            }
        }
    }
    

    查询商品信息界面

    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;
    using System.Data.SqlClient;
    using System.Configuration;
    
    namespace WindowsFormsApplication2
    {
        public partial class chaxun : Form
        {
            private string goodsid = null;
            public chaxun(string id)
            {
                this.goodsid = id;
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                String connStr = ConfigurationManager.ConnectionStrings["cxy2017270402"].ConnectionString;
                SqlConnection sqlConn = new SqlConnection(connStr);
                try
                {
                    //连接数据库
                    sqlConn.Open();
    
                    //构造命令
                    String sqlStr = "select * from 商品 where 1=1";
                   
    
                    // 添加查询条件
                    if (this.textBox1.Text.Trim().Equals(""))
                    {
                        sqlStr += "and ID='" + this.textBox1.Text.Trim() + "'";
                    }
                    if (this.textBox2.Text.Trim().Equals(""))
                    {
                        sqlStr += "and NAME like '%" + this.textBox2.Text.Trim() + "%'";
                    }
                    SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
                   
                    //将该查询过程绑定到DataAdaptater
                    SqlDataAdapter adp = new SqlDataAdapter();
                    adp.SelectCommand = cmd;
                    
                    //将datasethe 和dataadapter绑定
                    DataSet ds = new DataSet();
                    //自定义一个表()来标识数据库的商品表
                    adp.Fill(ds,"商品");
                    //指定DataGridView的数据源为DataSet的商品表
                    this.dataGridView1.DataSource = ds.Tables["商品"];
    
    
                   
                }
                catch (Exception exp)
                {
                    MessageBox.Show("访问数据库错误:" + exp.ToString());
                }
                finally
                {
                    sqlConn.Close();
                }
            }
            //数据修改,删除
            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                //点击修改链接
                if (e.RowIndex != -1 && e.ColumnIndex == 0)
                {
                    string goodsid = this.dataGridView1["商品条码", e.RowIndex].Value.ToString();
                    xiugai xiugai = new xiugai(goodsid);
                    xiugai.Show();
                }
                else if (e.RowIndex != -1 && e.ColumnIndex == 1)
                {
                    if (MessageBox.Show("确认删除?", "提示", MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        //获取所要删除关联对象的主键
                        string goodsid = this.dataGridView1["商品条码", e.RowIndex].Value.ToString();
                       
                        //连接字符串,注意与实际环境保持一致
                        String connStr = ConfigurationManager.ConnectionStrings["cxy2017270402"].ConnectionString;
                        SqlConnection sqlConn = new SqlConnection(connStr);
    
                        try
                        {
                            // 连接数据库
                            sqlConn.Open();
    
                            // 构造命令
                            String sqlStr = "dalata from 商品 where ID=@id";
                            SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
                            // SQL字符串参数赋值
                            cmd.Parameters.Add(new SqlParameter("@id", goodsid));            
                            // 将命令发送给数据库
                            int res = cmd.ExecuteNonQuery();
                            // 根据返回值判断是否修改成功
                            if (res != 0)
                            {
                                MessageBox.Show("商品信息修改成功");
                                this.Close();
                            }
                            else
                            {
                                MessageBox.Show("商品信息修改失败");
                            }
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show("访问数据库错误:" + exp.Message);
                        }
                        finally
                        {
                            sqlConn.Close();
                        }
                    }
                }          
            }
        }
    }
    

    商品修改界面

    image.png
    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;
    using System.Data.SqlClient;
    using System.Configuration;
    
    
    namespace WindowsFormsApplication2
    {
        public partial class xiugai : Form
        {
            private string goodsid = null;
            public xiugai(string id)
            {
                this.goodsid = id;
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                // 连接字符串,注意与实际环境保持一致
                String connStr = ConfigurationManager.ConnectionStrings["cxy2017270402"].ConnectionString;
                SqlConnection sqlConn = new SqlConnection(connStr);
                try
                {
                    // 连接数据库
                    sqlConn.Open();
    
                    // 构造命令发送给数据库
                    String sqlStr = "select * from 商品 where ID=@id";
                    SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
    
                    // SQL字符串参数赋值
                    cmd.Parameters.Add(new SqlParameter("@id", this.goodsid));
    
                    SqlDataReader dr = cmd.ExecuteReader();
    
                    // 如果从数据库中查询到记录,则显示这些数据
                    if (dr.HasRows)
                    {
                        dr.Read();
                        this.textBox1.Text = dr["ID"].ToString();
                        this.textBox2.Text = dr["NAME"].ToString();
                        this.textBox3.Text = dr["PRICE"].ToString();
                        this.textBox4.Text = dr["SPEC"].ToString();
                        this.textBox5.Text = dr["REMARK"].ToString();
                    }
                    else
                    {
                        MessageBox.Show("未找到相关记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (Exception exp)
                {
                    MessageBox.Show("访问数据库错误:" + exp.Message);
                }
                finally
                {
                    sqlConn.Close();
                }
            }
    
            private void bt_Ok_Click(object sender, EventArgs e)
            {
                String id = this.textBox1.Text.Trim();
                String name = this.textBox2.Text.Trim();
                float price = float.Parse(this.textBox3.Text.Trim());
                String spec = this.textBox4.Text.Trim();
                String remark = this.textBox5.Text.Trim();
    
                // 连接字符串,注意与实际环境保持一致
                String connStr = ConfigurationManager.ConnectionStrings["cxy2017270402"].ConnectionString;
                SqlConnection sqlConn = new SqlConnection(connStr);
                try
                {
                    // 连接数据库
                    sqlConn.Open();
    
                    // 构造命令
                    String sqlStr = "update 商品 set NAME=@name, PRICE=@price, SPEC=@spec, REMARK=@remark where ID=@id";
                    SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
    
                    // SQL字符串参数赋值
                    cmd.Parameters.Add(new SqlParameter("@id", id));
                    cmd.Parameters.Add(new SqlParameter("@name", name));
                    cmd.Parameters.Add(new SqlParameter("@price", price));
                    cmd.Parameters.Add(new SqlParameter("@spec", spec));
                    cmd.Parameters.Add(new SqlParameter("@remark", remark));
    
                    // 将命令发送给数据库
                    int res = cmd.ExecuteNonQuery();
    
                    // 根据返回值判断是否修改成功
                    if (res != 0)
                    {
                        MessageBox.Show("商品信息修改成功");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("商品信息修改失败");
                    }
                }
                catch (Exception exp)
                {
                    MessageBox.Show("访问数据库错误:" + exp.Message);
                }
                finally
                {
                    sqlConn.Close();
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
    
    

    数据库代码

    create database cxy2017270402
    USE [cxy2017270402]
    
    GO
    /****** Object:  Table [dbo].[收银员]    Script Date: 11/26/2018 16:13:37 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[收银员](
        [id] [int] NOT NULL,
        [name] [varchar](20) NULL,
        [mima] [varchar](10) NULL,
        [tol] [varchar](25) NULL,
    PRIMARY KEY CLUSTERED 
    (
        [id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object:  Table [dbo].[商品]    Script Date: 11/26/2018 16:13:37 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[商品](
        [id] [varchar](50) NOT NULL,
        [name] [varchar](50) NULL,
        [price] [varchar](50) NULL,
        [spec] [varchar](50) NULL,
        [remark] [varchar](50) NULL,
     CONSTRAINT [PK_商品] PRIMARY KEY CLUSTERED 
    (
        [id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    INSERT [dbo].[商品] ([id], [name], [price], [spec], [remark]) VALUES (N'11111', N'cxy', N'111', N'111', N'')
    INSERT [dbo].[商品] ([id], [name], [price], [spec], [remark]) VALUES (N'5453453453', N'', N'', N'', N'')
    /****** Object:  Table [dbo].[库管员]    Script Date: 11/26/2018 16:13:37 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[库管员](
        [id] [int] NOT NULL,
        [name] [varchar](20) NULL,
        [mima] [varchar](50) NULL,
        [tol] [varchar](25) NULL,
    PRIMARY KEY CLUSTERED 
    (
        [id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    /****** Object:  Table [dbo].[雇员]    Script Date: 11/26/2018 16:13:37 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[雇员](
        [id] [varchar](50) NOT NULL,
        [name] [varchar](50) NULL,
        [password] [varchar](50) NULL,
        [phone] [varchar](50) NULL,
        [type] [varchar](50) NULL,
     CONSTRAINT [PK_雇员] PRIMARY KEY CLUSTERED 
    (
        [id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    INSERT [dbo].[雇员] ([id], [name], [password], [phone], [type]) VALUES (N'1001', N'11', N'222', N'110', N'收银员')
    INSERT [dbo].[雇员] ([id], [name], [password], [phone], [type]) VALUES (N'2001', N'lp', N'000', N'120', N'库管员')
    /****** Object:  Table [dbo].[供应商]    Script Date: 11/26/2018 16:13:37 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_PADDING ON
    GO
    CREATE TABLE [dbo].[供应商](
        [id] [int] NOT NULL,
        [name] [varchar](20) NULL,
        [didian] [varchar](50) NULL,
        [lianxiren] [varchar](50) NULL,
        [tol] [varchar](25) NULL,
    PRIMARY KEY CLUSTERED 
    (
        [id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    SET ANSI_PADDING OFF
    GO
    

    相关文章

      网友评论

          本文标题:智慧商超登录系统

          本文链接:https://www.haomeiwen.com/subject/igjvcqtx.html