多字段精确查询是指浏览者根据程序提供的多个字段输入或选择查询关键字进行精确查询。多字段精确查询通常包括两个或两个以上用于指定查询条件的文本框或下拉列表框。无论查询条件是由文本框提供,还是由下拉列表框提供,其实现的方法是类似的。下面将以多列表查询为例介绍多字段精确查询的实现。
1.方案分析
多列表查询是指在一个查询中,可以包含两个或两个以上的下拉列表框。浏览者不但可以根据某一个列表框的值进行查询,还可以根据多个列表框的值组合后进行查询。通常应用在查询条件是某一类固定值的情况下。
在实现多列表查询时,首先需要确定检索词并选择查询条件,然后编写检索式将用户选择的多个查询条件组合为一个查询条件,最后输出查询结果。为了使读者更好地理解多列表查询,下面以带有3个列表框的多列表查询为例给出多列表查询的示意图,如图所示。
图 多列表查询的示意图
2.实施过程
在开发明日车辆管理系统时,制作出很多功能板块。在历史用车板块中,用户可以对数据进行多字段精确查询。用户可以对“用车单位”、“车牌号码”和“驾驶员”进行选择,然后在数据库中检索出符合这三个条件的所有数据,运行效果如图1和2所示。
图1 “历史用车”的所有记录
在用车单位的下拉列表中选择“国际学院音乐系“,车辆号码的下拉列表中选择”云c9919**“,驾驶员的下拉列表中选择”彭*伟“,单击【查询】按钮,查询出所有与查询条件相关的数据并显示出来,如图2所示。
图2 多字段精确查询得到的记录
程序实现具体步骤:
(1)新建一个网站命名为01,默认主页为Default.aspx。
(2)在Default.aspx页中添加两个TextBox控件,分别用于输入登录的用户名和密码,然后再添加一个Button控件用于登录。
(3)新建ASPX页,命名为car_jilu.aspx,在页面中添加3个DropDownList控件,分别用于设置查询用车单位、车辆号码和驾驶员。添加一个GridView控件用于显示查询结果数据。
(4)程序主要代码如下。
首先在Web.config文件中进行数据库连接配置,具体代码如下。
<appSettings>
<add key="ConnString" value="server=(local);uid=sa;pwd=;database=db_04"></add>
</appSettings>
进入后台主界面,单击左侧导航菜单中的“历史用车”,界面转到car_jilu.aspx页面。当此页面第一次加载时,调用usecar_DataBind方法将数据库中的相应数据绑定到显示“用车单位”、“车辆号码”和“驾驶员”的三个下拉列表,具体代码如下。
private void usecar_DataBind()
{
string SqlConn=System.Configuration.ConfigurationSettings.AppSettings["ConnString"];
SqlConnection Conn=new SqlConnection(SqlConn);
Conn.Open();
string SqlStr1="select id as '编号',carcode as '车牌号码',cartype as '车辆类型',caruser as '驾驶员',carcompany as '用车单位',carstate as '状态',carxingcheng as '车辆行程',carregtime as '用车日期',carmobile as '联系方式'from [beifen] order by id DESC";
SqlCommand Comm=new SqlCommand(SqlStr1,Conn);
DataSet ds=new DataSet();
SqlDataAdapter dr=new SqlDataAdapter(SqlStr1,Conn);
dr.Fill(ds,"beifen");
GridView1.DataSource=ds.Tables["beifen"].DefaultView;
GridView1.DataBind();
string SqlStr2="select * from [bumen]";
SqlDataAdapter de=new SqlDataAdapter(SqlStr2,Conn);
de.Fill(ds,"bumen");
this.DropDownList1.DataSource=ds.Tables["bumen"].DefaultView;
this.DropDownList1.DataTextField="type";
this.DropDownList1.DataValueField="type";
this.DropDownList1.DataBind();
string SqlStr3="select car_code from [car]";
SqlDataAdapter dw=new SqlDataAdapter(SqlStr3,Conn);
dw.Fill(ds,"carcode");
this.DropDownList2.DataSource=ds.Tables["carcode"].DefaultView;
this.DropDownList2.DataTextField="car_code";
this.DropDownList2.DataValueField="car_code";
this.DropDownList2.DataBind();
string SqlStr4="select name from [user] where part='院办(车队)'";
SqlDataAdapter dq=new SqlDataAdapter(SqlStr4,Conn);
dq.Fill(ds,"user");
this.DropDownList3.DataSource=ds.Tables["user"].DefaultView;
this.DropDownList3.DataTextField="name";
this.DropDownList3.DataValueField="name";
this.DropDownList3.DataBind();
Conn.Close();
}
当数据过多而需要分页的时候,通过下面的代码进行分页的数据绑定,具体代码如下。
private void GridView1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
DataBind();
}
当选择好查询条件,然后单击“查询”按钮进行查询,然后将查询结果绑定到GridView数据控件上显示出来,具体代码如下。
protected void Button3_Click(object sender, EventArgs e)
{
string SqlConn = System.Configuration.ConfigurationSettings.AppSettings["ConnString"];
SqlConnection Conn = new SqlConnection(SqlConn);
Conn.Open();
string SqlStr = "select id as '编号',carcode as '车牌号码',cartype as '车辆类型',caruser as '驾驶员',carcompany as '用车单位',carstate as '状态',carxingcheng as '车辆行程',carregtime as '用车日期',carmobile as '联系方式'from [beifen] where caruser='" + this.DropDownList3.SelectedValue + "' and carcompany like '%" + this.DropDownList1.SelectedValue + "%' and carcode='" + this.DropDownList2.SelectedValue + "' order by id DESC";
DataSet ds = new DataSet();
SqlDataAdapter dr = new SqlDataAdapter(SqlStr, Conn);
dr.Fill(ds, "beifen");
GridView1.DataSource = ds.Tables["beifen"].DefaultView;
GridView1.DataBind();
Conn.Close();
}
——摘自《C#编程词典》