智慧商超登錄系統(tǒng)

項(xiàng)目簡介

智慧社區(qū)是社區(qū)管理的一種新理念,是新形勢下社會(huì)管理創(chuàng)新的一種新模式。
智慧社區(qū)是指充分利用物聯(lián)網(wǎng)、云計(jì)算、移動(dòng)互聯(lián)網(wǎng)等新一代信息技術(shù)的集成 應(yīng)用,為社區(qū)居民提供一個(gè)安全、舒適、便利的現(xiàn)代化、智慧化生活環(huán)境,從而 形成基于信息化、智能化社會(huì)管理與服務(wù)的一種新的管理形態(tài)的社區(qū)。
智慧社區(qū)服務(wù)的內(nèi)容一般包括:智慧物業(yè)管理、電子商務(wù)服務(wù)、智慧養(yǎng)老服 務(wù)、智慧家居等。

制作成功的登錄界面

image.png

其對應(yīng)代碼

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("用戶名或者密碼錯(cuò)誤", "提示", 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("用戶名或者密碼錯(cuò)誤", "提示", 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
            {
                //連接數(shù)據(jù)庫
                sqlConn.Open();
                String sqlStr = "select * from 雇員 where ID=@id and PASSWORD=@pwd";
                SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);

                //注意用用戶ID登錄,而不是用戶名,用戶民可能會(huì)重復(fù)
                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("用戶名或密碼錯(cuò)誤", "提示", 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登錄,而不是用戶名,用戶民可能會(huì)重復(fù)
                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("數(shù)據(jù)庫訪問出錯(cuò)");
                }
                finally
                {
                    sqlConn.Close();
                }
            
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("請聯(lián)系管理員", "提示", 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);
            }
        }
    }
}

對應(yīng)代碼

收銀員界面

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("確認(rèn)注銷", "提示", MessageBoxButtons.OKCancel,
                MessageBoxIcon.Question) == DialogResult.OK)
            {
                //重新啟動(dòng)程序
                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

對應(yīng)代碼

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("確認(rèn)注銷", "提示", MessageBoxButtons.OKCancel,
               MessageBoxIcon.Question) == DialogResult.OK)
            {
                //重新啟動(dòng)程序
                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 修改個(gè)人信息IToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.修改個(gè)人信息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();

            //驗(yàn)證輸入信息
            if(newPwd.Equals(""))
            {
                MessageBox.Show("請輸入密碼","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            else if (confPwd.Equals(""))
            {
                MessageBox.Show("請輸入確認(rèn)密碼", "提示", 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
            {
                //連接數(shù)據(jù)庫
                sqlConn.Open();
                
                //構(gòu)造命令
                String sqlStr = "update 雇員 set PASSWORD=@pwd where ID=@id";
                SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
                
                // SQL字符串參數(shù)賦值
                cmd.Parameters.Add(new SqlParameter("@pwd", newPwd));
                cmd.Parameters.Add(new SqlParameter("@id", UserInfo.userId));

                // 將命令發(fā)送給數(shù)據(jù)庫
                int res = cmd.ExecuteNonQuery();

                // 根據(jù)返回值判斷是否修改成功
                if (res != 0)
                {
                    MessageBox.Show("密碼修改成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("密碼修改錯(cuò)誤");
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("訪問數(shù)據(jù)庫錯(cuò)誤:" + 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();
            // 連接字符串,注意與實(shí)際環(huán)境保持一致 
            String connStr = ConfigurationManager.ConnectionStrings["cxy2017270402"].ConnectionString; 
            SqlConnection sqlConn = new SqlConnection(connStr);
            try
            {
                //連接數(shù)據(jù)庫
                sqlConn.Open();
                //構(gòu)造命令
                String sqlStr = "insert into 商品(ID,NAME,PRICE,SPEC,REMARK) values(@id,@name,@price,@spec,@remark)";
                SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
                //SQL字符串參數(shù)賦值
                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));
                //將命令發(fā)送給數(shù)據(jù)庫
                int res = cmd.ExecuteNonQuery();
                //根據(jù)返回值判斷是否插入成功
                if (res != 0)
                {
                    MessageBox.Show("商品錄入成功");
                }
                else
                {
                    MessageBox.Show("商品錄入失敗");
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("訪問數(shù)據(jù)庫錯(cuò)誤:" + 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
            {
                //連接數(shù)據(jù)庫
                sqlConn.Open();

                //構(gòu)造命令
                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();
                //自定義一個(gè)表()來標(biāo)識(shí)數(shù)據(jù)庫的商品表
                adp.Fill(ds,"商品");
                //指定DataGridView的數(shù)據(jù)源為DataSet的商品表
                this.dataGridView1.DataSource = ds.Tables["商品"];


               
            }
            catch (Exception exp)
            {
                MessageBox.Show("訪問數(shù)據(jù)庫錯(cuò)誤:" + exp.ToString());
            }
            finally
            {
                sqlConn.Close();
            }
        }
        //數(shù)據(jù)修改,刪除
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //點(diǎn)擊修改鏈接
            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("確認(rèn)刪除?", "提示", MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK)
                {
                    //獲取所要?jiǎng)h除關(guān)聯(lián)對象的主鍵
                    string goodsid = this.dataGridView1["商品條碼", e.RowIndex].Value.ToString();
                   
                    //連接字符串,注意與實(shí)際環(huán)境保持一致
                    String connStr = ConfigurationManager.ConnectionStrings["cxy2017270402"].ConnectionString;
                    SqlConnection sqlConn = new SqlConnection(connStr);

                    try
                    {
                        // 連接數(shù)據(jù)庫
                        sqlConn.Open();

                        // 構(gòu)造命令
                        String sqlStr = "dalata from 商品 where ID=@id";
                        SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
                        // SQL字符串參數(shù)賦值
                        cmd.Parameters.Add(new SqlParameter("@id", goodsid));            
                        // 將命令發(fā)送給數(shù)據(jù)庫
                        int res = cmd.ExecuteNonQuery();
                        // 根據(jù)返回值判斷是否修改成功
                        if (res != 0)
                        {
                            MessageBox.Show("商品信息修改成功");
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("商品信息修改失敗");
                        }
                    }
                    catch (Exception exp)
                    {
                        MessageBox.Show("訪問數(shù)據(jù)庫錯(cuò)誤:" + 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)
        {

            // 連接字符串,注意與實(shí)際環(huán)境保持一致
            String connStr = ConfigurationManager.ConnectionStrings["cxy2017270402"].ConnectionString;
            SqlConnection sqlConn = new SqlConnection(connStr);
            try
            {
                // 連接數(shù)據(jù)庫
                sqlConn.Open();

                // 構(gòu)造命令發(fā)送給數(shù)據(jù)庫
                String sqlStr = "select * from 商品 where ID=@id";
                SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);

                // SQL字符串參數(shù)賦值
                cmd.Parameters.Add(new SqlParameter("@id", this.goodsid));

                SqlDataReader dr = cmd.ExecuteReader();

                // 如果從數(shù)據(jù)庫中查詢到記錄,則顯示這些數(shù)據(jù)
                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("未找到相關(guān)記錄", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("訪問數(shù)據(jù)庫錯(cuò)誤:" + 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();

            // 連接字符串,注意與實(shí)際環(huán)境保持一致
            String connStr = ConfigurationManager.ConnectionStrings["cxy2017270402"].ConnectionString;
            SqlConnection sqlConn = new SqlConnection(connStr);
            try
            {
                // 連接數(shù)據(jù)庫
                sqlConn.Open();

                // 構(gòu)造命令
                String sqlStr = "update 商品 set NAME=@name, PRICE=@price, SPEC=@spec, REMARK=@remark where ID=@id";
                SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);

                // SQL字符串參數(shù)賦值
                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));

                // 將命令發(fā)送給數(shù)據(jù)庫
                int res = cmd.ExecuteNonQuery();

                // 根據(jù)返回值判斷是否修改成功
                if (res != 0)
                {
                    MessageBox.Show("商品信息修改成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("商品信息修改失敗");
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show("訪問數(shù)據(jù)庫錯(cuò)誤:" + exp.Message);
            }
            finally
            {
                sqlConn.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

數(shù)據(jù)庫代碼

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].[供應(yīng)商]    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].[供應(yīng)商](
    [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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容