汽车信息查询系统

这是一个使用C#编写的汽车信息查询系统,通过DataSet和SqlDataAdapter从数据库获取数据。用户可以根据品牌、排量进行筛选,系统提供了加载数据、查询、重置和退出功能。在查询过程中,系统对可能出现的异常进行了捕获和处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Car
{
    public partial class frmCheck : Form
    {
        DataSet dataset;
        SqlDataAdapter dataAdapter;
        public frmCheck()
        {
            InitializeComponent();
        }
        private void frmCheck_Load(object sender, EventArgs e)
        {
            string sql = "SELECT CarsID,Brand,Type,Discharge,GearBox,OilUser,Price from CarsInfo";
            dataAdapter = new SqlDataAdapter(sql, DBHelper.connection);
            dataset = new DataSet("CarsManager");
            dataAdapter.Fill(dataset, "CarsInfo");
            dgvCheck.DataSource=dataset.Tables["CarsInfo"];
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (cboType.Text.Trim()=="全部")
            {
                txtTrem.ReadOnly = true;
                dataset.Tables["CarsInfo"].Clear();
                string sql = "SELECT * FROM CarsInfo";
                try
                {
                    dataAdapter = new SqlDataAdapter(sql,DBHelper.connection);
                    DBHelper.connection.Open();
                    dataAdapter.Fill(dataset, "CarsInfo");
                    dgvCheck.DataSource=dataset.Tables["CarsInfo"];
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    DBHelper.connection.Close();
                }
            }
            if (cboType.Text.Trim() == "品牌")
            {
                txtTrem.ReadOnly=false;
                dataset.Tables["CarsInfo"].Clear();
                string sql = string.Format("SELECT * from CarsInfo where brand='{0}'",txtTrem.Text);
               
                try
                {
                    dataAdapter=new SqlDataAdapter(sql,DBHelper.connection);
                    DBHelper.connection.Open();
                    dataAdapter.Fill(dataset,"CarsInfo");
                    dgvCheck.DataSource=dataset.Tables["CarsInfo"];
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    DBHelper.connection.Close();
                }
            }
            if (cboType.Text.Trim()=="排量")
            {
                txtTrem.ReadOnly = false;
                dataset.Tables["CarsInfo"].Clear();
                string sql = string.Format("SELECT CarsID,Brand,Type,Discharge,GearBox,OilUser,Price from CarsInfo where Discharge='{0}'", txtTrem.Text);
                try
                {
                    dataAdapter=new SqlDataAdapter(sql,DBHelper.connection);
                    DBHelper.connection.Open();
                    dataAdapter.Fill(dataset,"CarsInfo");
                    dgvCheck.DataSource=dataset.Tables["CarsInfo"];
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    DBHelper.connection.Close();
                }
            }
            else if (cboType.Text.Trim() == " ")
            {
                MessageBox.Show("请输入查询条件!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void btnRest_Click(object sender, EventArgs e)
        {
            dataset.Tables["CarsInfo"].Clear();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值