当前位置: 首页 > news >正文

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];}}}
}
http://www.lryc.cn/news/92744.html

相关文章:

  • Nautilus Chain全球行分享会,上海站圆满举办
  • day50_mybatis
  • 第十一届“创业江苏”科技创业大赛正式启动
  • EasyX实现简易贪吃蛇
  • Linux下ElasticSearch7.9.2安装配置(包含服务器配置、启动停止脚本、开放端口和elasticsearch-head插件的使用)
  • JS 之 事件Event对象详解(属性、方法、自定义事件)
  • 65寸电视长宽多少厘米
  • Python爬取影评并进行情感分析和数据可视化
  • ubuntu22.04.2安装onlyoffice(不更改默认端口版)
  • 企业如何有效制定企业信息化发展规划?(附信息化模板)
  • 计算机网络填空题
  • 【HashMap】为什么用自定义的类做HashMap的Key时需要重写hashcode方法和equals方法
  • Flutter自定义对话框返回相关问题汇总
  • 002docker 安装
  • 软件工程师,全面思考问题很重要
  • 1.Apollo部署-linux
  • 【HTML】form标签
  • 基于SPAD / SiPM技术的激光雷达方案
  • 使用MATLAB工具模拟单/双频GPS和载波相位差分GPS
  • 当社恐成为技术面试官
  • Jetpack Compose:使用PagerIndicator和Infinity实现滚动的HorizontalPager
  • 2023年杭州/广州/东莞/深圳软考(中/高级)认证,618报名特惠
  • springboot项目外卖管理 day03-公共字段填充与新增删除分类
  • Nginx:Tomcat部署及优化(一)
  • Docker Swarm 集群搭建和使用 —— 筑梦之路
  • 是否需要更换CRM系统如何评估?如何确保更换成功?
  • CSDN竞赛57期题解
  • springboot+vue.js大学生竞赛报名作品评分管理系统
  • Python爱好者的自我修养(1):简单输入与输出
  • java SSM 摄影作品网站myeclipse开发mysql数据库springMVC模式java编程计算机网页设计