组合查询,
- 界面
界面和第一次机房没有太大的变化
- 代码
B层部分代码
//建立一个哈希表,输入键和值,传给D层,通过调用list集合,显示对应的值
Hashtable hash = new Hashtable(); //创建一个哈希表
//在哈希表中添加数据
hash.Add("用户信息", "UserInfo");
hash.Add("充值记录", "Recharge");
hash.Add("上机记录", "Line");
hash.Add("正在上机人员", "Line");
hash.Add("账号","UserID");
hash.Add("姓名", "UserName");
hash.Add("注册日期", "Date");
hash.Add("用户类型", "Level");
hash.Add("充值日期", "AddDateTime");
hash.Add("上机日期", "OnDate");
hash.Add("下机日期", "OffDate");
hash.Add("用户级别", "Type");
hash.Add("与", "AND");
hash.Add("或", "OR");
hash.Add("<", "<");
hash.Add(">", ">");
hash.Add("<>", "!=");
hash.Add("=", "=");
hash.Add(list[3],list[3]); //在哈希表中添加第一行查询内容的数据
if (list.Count>7) //如果集合数量大于7,证明选择了第二行查询条件
{
hash.Add(list[7], list[7]); //哈希表中添加第二行查询内容的数据
}
if (list.Count > 11) //如果集合数量大于11,证明选择了第三行查询条件
{
hash.Add(list[11], list[11]); //哈希表中添加第三行查询内容的数据
}
DataTable table = user.Compositequery(hash,list); //将哈希表和集合传给D层
return table;
D层代码
SQLHelper sqlhelper = new SQLHelper();
//SqlParameter[] sqlparams ={};
SqlCommand cmd = conn.CreateCommand();
string str = hash[list[2]].ToString();
string sql = @" SELECT * fROM "+hash[list[0]];
//根据用户的选择,去查询不同的表的数据
if (list[0].ToString().Trim()=="用户信息")
{
sql += " WHERE "+hash[list[1]].ToString().Trim()+" "+hash[list[2]]+" '" +hash[list[3]].ToString().Trim()+"' ";
if (list.Count >=5) //查询两行条件
{
sql = sql + " "+ hash[list[4]].ToString().Trim()+" "+ hash[list[5]].ToString().Trim() + " " + hash[list[6]].ToString().Trim() + " '" + hash[list[7]].ToString().Trim()+"' ";
}
if (list.Count>=9)
{
sql=sql+" "+ hash[list[8]].ToString().Trim() +" "+ hash[list[9]].ToString().Trim() + " " + hash[list[10]].ToString().Trim() + " '" + hash[list[11]].ToString().Trim()+"' ";
}
}
cmd.CommandType = CommandType.Text;
DataTable table = sqlhelper.Query(sql, CommandType.Text);