目录
题目:
效果图:
数据库:
做法:
combobox值更新
查询按钮功能(非空验证,查询数据)
datagirdview设置
全部代码:
DBHelper类
From1主窗体代码
当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];
}
首先设置datagridview的这三个属性
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;
}
}
}
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];
}
}
}
}