C#可视化 家用轿车信息查询系统(具体做法及全部代码)

目录

题目:

 效果图:

 数据库:

做法:

combobox值更新

 查询按钮功能(非空验证,查询数据)

datagirdview设置

全部代码: 

 DBHelper类

 From1主窗体代码


题目:

 

 

 效果图:

                                               

 数据库:

 

 

做法:

combobox值更新

当comboBox1.text的文本为排量和售价时,显示两个文本框,其他情况仅显示一个文本框。每次选择完节点后两个文本框的值设置为空

 

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text=="品牌"||comboBox1.Text == "型号"|| comboBox1.Text == "变速箱")
            {
                textBox1.Text= "";
                textBox2.Text = "";
                label4.Hide();
                textBox2.Hide();
            }
           
            if (comboBox1.Text == "排量"|| comboBox1.Text == "售价")
            {
                textBox1.Text = "";
                textBox2.Text = "";
                label4.Show();
                textBox2.Show();
            }
        }

 查询按钮功能(非空验证,查询数据)

非空验证:当点击查询按钮时,查询种类combobox不能为空,当combobox有值时,如果查询种类为品牌、型号、变速箱则第一个不能为空,如果查询种类为排量、售价时两个文本框都不能为空

 private void button1_Click(object sender, EventArgs e)
        {
            //comboBox非空验证
            if (comboBox1.Text=="")
            {
                MessageBox.Show("请选择查询种类!");
                return;
            }
            //textbox非空验证
            if (comboBox1.Text=="排量"|| comboBox1.Text == "售价" )
            {
                if (textBox1.Text==""||textBox2.Text=="")
                {
                    MessageBox.Show("查询条件输入不完整!");
                    return;
                }
               
            }
            if (comboBox1.Text == "品牌" || comboBox1.Text == "型号" || comboBox1.Text == "变速箱")
            {
                if (textBox1.Text == "" )
                {
                    MessageBox.Show("查询条件输入不完整!");
                    return;
                }
                
            }
           

查询数据: 

查询种类为品牌、型号、变速箱时,定义字符串用来存数据库中的对应列名,列名有了后根据textbox进行模糊查询并显示就好了。

查询种类为排量、售价时,定义字符串用来存数据库中的对应列名,列名有了后根据textbox1和textbox2进行between and区间查询就好了。

因为排量时小数,定义变量直接就用的double类型:

double qi = Convert.ToDouble(textBox1.Text);

 double z = Convert.ToDouble(textBox2.Text);


            //如果是这三种情况,拿到选项对应数据库中的列进行查询
            if (comboBox1.Text == "品牌" || comboBox1.Text == "型号" || comboBox1.Text == "变速箱") {
                string x = "";
                if (comboBox1.Text=="品牌")
                {
                    x = "brand";
                }
                else if (comboBox1.Text == "型号")
                {
                    x = "type";
                }
                else
                {
                    x = "gearbox";
                }
                
                
                string sql = string.Format("select * from tb_car where {0} like '%{1}%'",x,textBox1.Text);
               this.dataGridView1.DataSource= DBHelper.ds(sql).Tables[0];

            }
            //如果是这两种情况,拿到选项对应数据库中的列进行查询
            else if (comboBox1.Text == "排量" || comboBox1.Text == "售价")
            {
                string tiaoJian = "";
                  
                if (comboBox1.Text=="排量")
                {
                    tiaoJian = "discharge";
                }
                else
                {
                    tiaoJian = "price";
                }
                double qi = Convert.ToDouble(textBox1.Text);
                double z = Convert.ToDouble(textBox2.Text);
                string sql = string.Format("select * from tb_car where {0} between {1} and {2}", tiaoJian,qi,z);

                this.dataGridView1.DataSource = DBHelper.ds(sql).Tables[0];
            }
            else
            {
                string sql= "select * from tb_car";
              this.dataGridView1.DataSource=  DBHelper.ds(sql).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 WindowsFormsApp1
{
    internal class DBHelper
    {
        public static string connstr = "server=.;database=CarDBj;uid=sa;pwd=123456";
        public static SqlConnection conn = null;
        public static void init() {
            if (conn==null)
            {
                conn=new SqlConnection(connstr);
            }
            conn.Close();
            conn.Open();
        
        }
        public static bool noqery(string sql) { 
        init();
        SqlCommand cod=new SqlCommand(sql,conn);
            if (cod.ExecuteNonQuery()>0)
            {
                conn.Close();
                return true;
            }
            else
            {
                conn.Close();
                return false;
            }
        }
        public static DataSet ds(string sql) {
            init();
            DataSet ds = new DataSet();
            SqlDataAdapter t = new SqlDataAdapter(sql ,conn);
        t.Fill(ds);
            conn.Close();
            return ds;
        
        }


    }
}

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

        private void Form1_Load(object sender, EventArgs e)
        {
            string sql = "select * from  tb_car";
         this.dataGridView1.DataSource=   DBHelper.ds(sql).Tables[0];


            label4.Hide();
            textBox2.Hide();
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text=="品牌"||comboBox1.Text == "型号"|| comboBox1.Text == "变速箱")
            {
                textBox1.Text= "";
                textBox2.Text = "";
                label4.Hide();
                textBox2.Hide();
            }
           
            if (comboBox1.Text == "排量"|| comboBox1.Text == "售价")
            {
                textBox1.Text = "";
                textBox2.Text = "";
                label4.Show();
                textBox2.Show();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //comboBox非空验证
            if (comboBox1.Text=="")
            {
                MessageBox.Show("请选择查询种类!");
                return;
            }
            //textbox非空验证
            if (comboBox1.Text=="排量"|| comboBox1.Text == "售价" )
            {
                if (textBox1.Text==""||textBox2.Text=="")
                {
                    MessageBox.Show("查询条件输入不完整!");
                    return;
                }
               
            }
            if (comboBox1.Text == "品牌" || comboBox1.Text == "型号" || comboBox1.Text == "变速箱")
            {
                if (textBox1.Text == "" )
                {
                    MessageBox.Show("查询条件输入不完整!");
                    return;
                }
                
            }




            //如果是这三种情况,拿到选项对应数据库中的列进行查询
            if (comboBox1.Text == "品牌" || comboBox1.Text == "型号" || comboBox1.Text == "变速箱") {
                string x = "";
                if (comboBox1.Text=="品牌")
                {
                    x = "brand";
                }
                else if (comboBox1.Text == "型号")
                {
                    x = "type";
                }
                else
                {
                    x = "gearbox";
                }
                
                
                string sql = string.Format("select * from tb_car where {0} like '%{1}%'",x,textBox1.Text);
               this.dataGridView1.DataSource= DBHelper.ds(sql).Tables[0];

            }
            //如果是这两种情况,拿到选项对应数据库中的列进行查询
            else if (comboBox1.Text == "排量" || comboBox1.Text == "售价")
            {
                string tiaoJian = "";
                  
                if (comboBox1.Text=="排量")
                {
                    tiaoJian = "discharge";
                }
                else
                {
                    tiaoJian = "price";
                }
                double qi = Convert.ToDouble(textBox1.Text);
                double z = Convert.ToDouble(textBox2.Text);
                string sql = string.Format("select * from tb_car where {0} between {1} and {2}", tiaoJian,qi,z);

                this.dataGridView1.DataSource = DBHelper.ds(sql).Tables[0];
            }
            else
            {
                string sql= "select * from tb_car";
              this.dataGridView1.DataSource=  DBHelper.ds(sql).Tables[0];



            }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

W少年没有乌托邦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值