C#手机超市查询系统

9 篇文章 0 订阅
4 篇文章 0 订阅

这是一个简单的手机查询系统

        查所有

       按查询手机类型查询

        按品牌进行查询

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;

namespace FrmMobileManager
{
    public partial class Form1 : Form
    {
        DBHelper helper = new DBHelper();
        DataSet ds = null;
        DataView dv = null;
        SqlDataAdapter adapter = null;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void clear_Click(object sender, EventArgs e)
        {
            this.cboTypes.Text ="全部";
            this.txtQuery.Clear();
        }

        private void close_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定退出吗?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
            if (result==DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void select_Click(object sender, EventArgs e)
        {
            if (Band()==true)
            {
                if (this.cboTypes.Text.Trim().Equals("全部")&&this.txtQuery.Text.Trim()==string.Empty)
                {
                    Form1_Load(sender,e);
                }
                if (this.cboTypes.Text.Trim().Equals("手机类型")&&this.txtQuery.Text.Trim()!=null)
                {
                     SelectCategory();
                }
                if (this.cboTypes.Text.Trim().Equals("品牌")&& this.txtQuery.Text.Trim()!=null)
                {
                    SelectType();
                }
            }
        }

        private void SelectType()
        {
            try
            {
                string sql = string.Format(@"SELECT [Mid]as '序号'
                                              ,[Bland]as '品牌'
                                              ,[Type]as '型号'
                                              ,[price]as '价格'
                                              ,CategoryInfo.Category as '手机类型'
                                          FROM [MobileInfo]inner join CategoryInfo on MobileInfo.Cid=CategoryInfo.Cid
                                          where Bland='{0}'", this.txtQuery.Text.Trim());
                adapter = new SqlDataAdapter(sql, helper.Connection);
                ds = new DataSet();
                if (ds.Tables["MobileInfo"] != null)
                {
                    ds.Tables["MobileInfo"].Clear();
                }
                adapter.Fill(ds, "MobileInfo");
                dv = new DataView(ds.Tables["MobileInfo"]);
                this.dgvSelect.DataSource = dv;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            
            }
         
        }
        private void SelectCategory()
        {
            string sql = string.Format(@"SELECT [Mid]as '序号'
                                          ,[Bland]as '品牌'
                                          ,[Type]as '型号'
                                          ,[price]as '价格'
                                          ,CategoryInfo.Category as '手机类型'
                                      FROM [MobileInfo]inner join CategoryInfo on MobileInfo.Cid=CategoryInfo.Cid
                                      where CategoryInfo.Category='{0}'",this.txtQuery.Text.Trim());
            adapter = new SqlDataAdapter(sql, helper.Connection);
            ds = new DataSet();
            if (ds.Tables["MobileInfo"] != null)
            {
                ds.Tables["MobileInfo"].Clear();
            }
            adapter.Fill(ds, "MobileInfo");
            dv = new DataView(ds.Tables["MobileInfo"]);
            this.dgvSelect.DataSource = dv;  
        }
        private bool Band()
        {
            if (this.cboTypes.Text.Trim()==string.Empty&&this.txtQuery.Text.Trim()==string.Empty)
            {
                MessageBox.Show("查询条件和查询种类都不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dgvSelect1();
        }

        private void dgvSelect1()
        {
            string sql = string.Format(@"SELECT [Mid]as '序号'
                                              ,[Bland]as '品牌'
                                              ,[Type]as '型号'
                                              ,[price]as '价格'
                                              ,CategoryInfo.Category as '手机类型'
                                          FROM [MobileInfo],CategoryInfo
                                          where MobileInfo.Cid=CategoryInfo.Cid");
            adapter = new SqlDataAdapter(sql, helper.Connection);
            ds = new DataSet();
            if (ds.Tables["MobileInfo"] != null)
            {
                ds.Tables["MobileInfo"].Clear();
            }
            adapter.Fill(ds, "MobileInfo");
            dv = new DataView(ds.Tables["MobileInfo"]);
            this.dgvSelect.DataSource = dv;  
        }

    }
}











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

namespace FrmMobileManager
{
    class DBHelper
    {
        //数据库四参数
        private string strConn = "Data Source=.;Initial Catalog=MobileManager;User ID=sa;Pwd=1234";
        //Connection对象
        private SqlConnection connection;
        public SqlConnection Connection
        {
            get
            {
                if (connection==null)
                {
                    connection=new SqlConnection(strConn);
                }
                return connection;
            }
        }
        public void OpenConnection()
        {
            if (Connection.State == ConnectionState.Closed)
            {
                Connection.Open();
            }
            else if (Connection.State == ConnectionState.Broken)
            {
                Connection.Close();
                Connection.Open();
            }
            }
        public void CloseConnection()
        {
            if (Connection.State == ConnectionState.Open || Connection.State == ConnectionState.Broken)
            {
                Connection.Close();
            }           
       }
    }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值