简单模糊查询

开发环境:Microsoft Visual Studio2005

数据库:SQL Server2000

步骤:

step1:新建一个项目Query

              文件→新建→项目→在项目类型中展开Visual C#→选中"Windows"→在模板中选择"Windows应用程序"→指定名称(Query)和位置(自定义)→确定

step2:设置主窗体(将其命名为mainFrm.cs)

              置IsMdiCotianer属性值为True   //设置该窗体成为多个子窗体的父窗体

step3:向主窗体添加菜单栏

              向窗体中添加menuStrip控件,在控件中输入"基本信息查询(&Q)"

 step4:新增一个窗体(名为queryFrm.cs)并在其中添加控件

控件属性
控件名属性
LabelNamelabel1
 Text查询条件
LabelNamelabel2
 Text字段名称
ComboBoxDropDownStyleDropDownList
textBox1Text 
button1Namebtnquery
 Text查询
DataGridViewNamedataGridView1
 AnchorTop, Bottom, Left, Right

step5:添加类operateDB

1、右击项目→添加→类(命名为operateDB.cs)
2、类的代码如下:
//operateDB类 用来操作数据库
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace studentMIS
{
    class operateDB
    {
        string strconn = "server=localhost;database=studentM;integrated security=sspi;pooling=false";//本地数据库
        SqlConnection conn = new SqlConnection(strconn);
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();

        public DataSet QueryDB(string strChoice,string strquery1,string strquery2, string strquery3,string strquery4)
        {
            if (strChoice == "学号")
            {
                cmd = conn.CreateCommand();
                da.SelectCommand = cmd;
                ds.Clear();
                cmd.CommandText = strquery1;
                da.Fill(ds, "table");
            }
            else if (strChoice == "姓名")
            {
                cmd = conn.CreateCommand();
                da.SelectCommand = cmd;
                ds.Clear();
                cmd.CommandText = strquery2;
                da.Fill(ds,"table");
            }
            else if (strChoice == "专业")
            {
                cmd = conn.CreateCommand();
                da.SelectCommand = cmd;
                ds.Clear();
                cmd.CommandText = strquery3;
                da.Fill(ds, "table");
            }
            else if (strChoice == "班级")
            {
                cmd = conn.CreateCommand();
                da.SelectCommand = cmd;
                ds.Clear();
                cmd.CommandText = strquery4;
                da.Fill(ds, "table");
            }
            return ds;
        }
    }
}

step6:运行时主窗体时如何显示查询窗体

               private void 基本信息查询ToolStripMenuItem_Click(object sender, EventArgs e)
               {
                     queryFrm qFrm = new queryFrm();
                     qFrm.MdiParent = this;
                     qFrm.Show();
                }

step7:查询窗体queryFrm.cs的代码

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

namespace Query
{
    public partial class queryFrm : Form
    {
        operateDB op_DB = new operateDB();
        DataSet myds = new DataSet();
        public queryFrm()
        {
            InitializeComponent();
        }

        private void btnquery_Click(object sender, EventArgs e)
        {
            string strxz =this.comboBox1.SelectedItem.ToString().Trim();
            string strcx1 = "select * from student where 学号 like '%" + this.textBox1.Text.ToString().Trim() +"%'";
            string strcx2 = "select * from student where 姓名 like '%" + this.textBox1.Text.ToString().Trim() + "%'";
            string strcx3 = "select * from student where 专业 like '%" + this.textBox1.Text.ToString().Trim() + "%'";
            string strcx4 = "select * from student where 班级 like '%" + this.textBox1.Text.ToString().Trim() + "%'";
            this.myds = this.op_DB.QueryDB(strxz,strcx1,strcx2, strcx3,strcx4);
            this.dataGridView1.DataSource=myds;
            this.dataGridView1.DataMember="table";
        }
    }
}

step8:备注:数据库使用的是SQL Server2000

创建了数据库studentMIS,里面有张student表(字段数据类型不一定符合,只用来测试用的)

表结构如下:

列名数据类型长度
学号char10
姓名char10
性别char2
出生日期datatime8
身份证号char18
专业char30
班级char4
家庭电话char15
家庭住址char50
住宿情况char2
住宿地址char50

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值