C#可视化 商品信息管理系统(具体做法及全部代码)

目录

题目:

 效果图:

 数据库:

做法:

主页面添加menustrip,之后添加两个窗体双击事件,双击下拉区域就好了。

添加界面

查询按钮功能

datagirdview设置

全部代码: 

 DBHelper类

 From1主窗体代码

添加代码:

查询代码 


题目:

 效果图:

信息管理下拉框中有两个选项,每一个都会打开对应的窗体

                                                   

 

 数据库:

 

做法:

主页面添加menustrip,之后添加两个窗体双击事件,双击下拉区域就好了。

添加界面

 

 该页面主要代码:

该代码作用:

1 首先为Brand表添加brand数据,

2 红色字体是全新写法,查询别的表的内容

 string s = string.Format("insert PcInfo(pcName,brandId,isReceiv,pcPrice,pcDetail,pcRemark)values ('{0}',(select id from Brand where brand='{1}'),{2},{3},'{4}','{5}')", textBox1.Text, comboBox1.Text, temp, textBox4.Text, textBox2.Text, textBox3.Text);

 private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text != null && textBox2.Text != null && textBox3.Text != null && textBox4.Text != null && comboBox1.Text != null)
                {
                    string sql = string.Format("insert Brand(brand)values ('{0}')", comboBox1.Text.ToString());
                    if (!DBHelper.noqe(sql))
                    {
                        MessageBox.Show("品牌添加失败");
                    }
                    byte temp = 0;
                    if (radioButton2.Checked == true)
                    {
                        temp =1;
                    }
                    string s = string.Format("insert PcInfo(pcName,brandId,isReceiv,pcPrice,pcDetail,pcRemark)values ('{0}',(select id from Brand where brand='{1}'),{2},{3},'{4}','{5}')", textBox1.Text, comboBox1.Text, temp, textBox4.Text, textBox2.Text, textBox3.Text);
                    if (DBHelper.noqe(s))
                    {
                        MessageBox.Show("保存成功!");
                    }
                    else
                    {
                        MessageBox.Show("保存失败!");
                    }
                }
                else
                {
                    MessageBox.Show("请录入完整商品信息!");

                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());              
            }
        }

查询按钮功能

需要用到连接查询,因为有两个表。再用模糊查询查询两个表的内容

private void button1_Click(object sender, EventArgs e)
        {
            string sql =string.Format("select pcName,brand,pcPrice,pcDetail,timestamp from PcInfo p join Brand b on p.brandId=b.id where pcName like'%{0}%'",textBox1.Text) ;
            DataSet ds = DBHelper.ds(sql);
            this.dataGridView1.DataSource = ds.Tables[0];
        }

datagirdview设置

首先设置datagridview的这三个属性

    1. AutoSizeColumsMode = Fill 设置每列单元格宽度平铺

    1. RowHeadersVisible = False 取消列表最左侧列显示

    1. SelectionMode = FullRowSelect 设置单元格选中模式为整行选中

 

全部代码: 

 DBHelper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace spxxgl
{
    class DBHelper
    {
        public static SqlConnection conn = null;
        public static string connstr = "server=.;database=GoodsManager;uid=sa;pwd=123456";
        public static void into() {
            if (conn==null)
            {
                conn = new SqlConnection(connstr);
            }
            conn.Close();
            conn.Open();
        }
        public static bool noqe(string sql) {
            into();
            SqlCommand cmd = new SqlCommand(sql,conn);
            int ret = cmd.ExecuteNonQuery();
            conn.Close();
            if (ret>0)
            {
                
                return true;
            }
            else
            {
                return false;
            }
        }
        public static DataSet ds(string sql){
            into();
            DataSet d = new DataSet();
            SqlDataAdapter t = new SqlDataAdapter(sql,conn);
            t.Fill(d);
            conn.Close();
            return d;



        }
    }
}

 From1主窗体代码

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 spxxgl
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 增加信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            jia j = new jia();
            j.ShowDialog();
            j.MdiParent = this;
           
        }

        private void 查询信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cha j = new cha();
            j.ShowDialog();
            j.MdiParent = this;
        }

        private void MainForm_Load(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;

namespace spxxgl
{
    public partial class jia : Form
    {
        public jia()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text != null && textBox2.Text != null && textBox3.Text != null && textBox4.Text != null && comboBox1.Text != null)
                {
                    string sql = string.Format("insert Brand(brand)values ('{0}')", comboBox1.Text.ToString());
                    if (!DBHelper.noqe(sql))
                    {
                        MessageBox.Show("品牌添加失败");
                    }
                    byte temp = 0;
                    if (radioButton2.Checked == true)
                    {
                        temp =1;
                    }
                    string s = string.Format("insert PcInfo(pcName,brandId,isReceiv,pcPrice,pcDetail,pcRemark)values ('{0}',(select id from Brand where brand='{1}'),{2},{3},'{4}','{5}')", textBox1.Text, comboBox1.Text, temp, textBox4.Text, textBox2.Text, textBox3.Text);
                    if (DBHelper.noqe(s))
                    {
                        MessageBox.Show("保存成功!");
                    }
                    else
                    {
                        MessageBox.Show("保存失败!");
                    }
                }
                else
                {
                    MessageBox.Show("请录入完整商品信息!");

                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());              
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = null;
            comboBox1.Text = null;
            radioButton1.Checked = true;
            textBox4.Text = null;
            textBox2.Text = null;
            textBox3.Text = null;
        }

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

        private void jia_Load(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;

namespace spxxgl
{
    public partial class cha : Form
    {
        public cha()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql =string.Format("select pcName,brand,pcPrice,pcDetail,timestamp from PcInfo p join Brand b on p.brandId=b.id where pcName like'%{0}%'",textBox1.Text) ;
            DataSet ds = DBHelper.ds(sql);
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        private void cha_Load(object sender, EventArgs e)
        {
            string sql = "select pcName,brand,pcPrice,pcDetail,timestamp from PcInfo p join Brand b on p.brandId=b.id";
          DataSet ds=  DBHelper.ds(sql);
           this.dataGridView1.DataSource= ds.Tables[0];
        }
    }
}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

W少年没有乌托邦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值