数据联动查询界面设计之一
今天抽空做了一下数据的联动查询界面。
1.list.aspx:
放置4个Combobox控件,ID分别为:cb1,cb2,cb3,cb4
2.list.aspx.cs:
主要实现页面载入(Page_Load)、控件与数据库相应字段的绑定以及几个控件的联动(cb1_TextChanged,cb3_TextChanged)。后台代码如下:
//名称空间引用
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class list : System.Web.UI.Page
{
//页面载入事件
//实现与数据库的联接、控件数据绑定初始化
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//数据库连接字符串设置,其中MapPath()取得站点根目录所在路径.
string strCon = "Provider=Microsoft.Jet.OleDb.4.0;";
strCon += "Data Source=";
strCon += MapPath("~/App_Data/db.mdb");
OleDbConnection oleCon = new OleDbConnection(strCon);
//设置四个控件对应的查询语句.
string strSel = "select * from 省份";
string strSel2 = "select * from 商业";
string strSel3 = "select * from 地区";
string strSel4 = "select * from 终端";
OleDbCommand myCom = new OleDbCommand(strSel, oleCon);
OleDbCommand myCom2 = new OleDbCommand(strSel2, oleCon);
OleDbCommand myCom3 = new OleDbCommand(strSel3, oleCon);
OleDbCommand myCom4 = new OleDbCommand(strSel4, oleCon);
oleCon.Open();
//为每个控件绑定相应数据.
OleDbDataReader myDR = myCom.ExecuteReader();
cb1.DataSource = myDR;
cb1.DataTextField = "省份";
cb1.DataBind();
myDR.Close();
OleDbDataReader myDR2 = myCom2.ExecuteReader();
cb2.DataSource = myDR2;
cb2.DataTextField = "商业名称";
cb2.DataBind();
myDR2.Close();
OleDbDataReader myDR3 = myCom3.ExecuteReader();
cb3.DataSource = myDR3;
cb3.DataTextField = "地区名称";
cb3.DataBind();
myDR3.Close();
OleDbDataReader myDR4 = myCom4.ExecuteReader();
cb4.DataSource = myDR4;
cb4.DataTextField = "终端名称";
cb4.DataBind();
myDR4.Close();
oleCon.Close();
}
}
//"省份"下拉框的文本改变事件,当省份改变时"商业"、"地区"、"终端名称"相应进行改变
protected void cb1_TextChanged(object sender, EventArgs e)
{
//数据库连接字符串
string strCon = "Provider=Microsoft.Jet.OleDb.4.0;";
strCon += "Data Source=";
strCon += MapPath("~/App_Data/db.mdb");
OleDbConnection oleCon = new OleDbConnection(strCon);
//产生联动的控件的查询代码,注意加入了对"省份"控件的文本值的引用
string strSel = "select * from 商业 where [省份] like '"+cb1.Text + "'";
string strSel2 = "select * from 地区 where [省份] like '" + cb1.Text + "'";
string strSel3 = "select * from 终端 where [省份] like '" + cb1.Text + "'";
OleDbCommand myCom = new OleDbCommand(strSel, oleCon);
OleDbCommand myCom2 = new OleDbCommand(strSel2, oleCon);
OleDbCommand myCom3 = new OleDbCommand(strSel3, oleCon);
oleCon.Open();
OleDbDataReader myDR = myCom.ExecuteReader();
cb2.DataSource = myDR;
cb2.DataTextField = "商业名称";
cb2.DataBind();
myDR.Close();
OleDbDataReader myDR2 = myCom2.ExecuteReader();
cb3.DataSource = myDR2;
cb3.DataTextField = "地区名称";
cb3.DataBind();
myDR2.Close();
OleDbDataReader myDR3 = myCom3.ExecuteReader();
cb4.DataSource = myDR3;
cb4.DataTextField = "终端名称";
cb4.DataBind();
myDR3.Close();
oleCon.Close();
}
//"地区"控件的文本改变事件,引起"终端"控件改变.
protected void cb3_TextChanged(object sender, EventArgs e)
{
string strCon = "Provider=Microsoft.Jet.OleDb.4.0;";
strCon += "Data Source=";
strCon += MapPath("~/App_Data/db.mdb");
OleDbConnection oleCon = new OleDbConnection(strCon);
string strSel = "select * from 终端 where [省份] like '" + cb1.Text + "' and [地区] like '" + cb3.Text +"'";
OleDbCommand myCom = new OleDbCommand(strSel, oleCon);
oleCon.Open();
OleDbDataReader myDR = myCom.ExecuteReader();
cb4.DataSource = myDR;
cb4.DataTextField = "终端名称";
cb4.DataBind();
myDR.Close();
oleCon.Close();
}
}
注意问题:
1.对名称空间的引用.
2.连接字符串中数据库文件所在路径的表示.
3.用同一连接对多个控件进行数据绑定时要注意DataReader的使用-前一个close之后才能创建第二个,否则会出错.
4.在查询语句中引用控件文本值的方式.
这次主要的收获是成功连接数据库并实现联动,代码看起来好象有点冗长,没办法,编程经验尚少,希望大侠们能多多指正!!