ASP.NET三层架构之不确定查询参数个数的查询
在做三层架构的时候,特别是对表做查询的时候,有时候并不确定查询条件的个数,比如查询学生表:有可能只输入学号,或者姓名,或者性别,总之查询条件的参数个数并不确定,下面是我用List实现传值的代码:
附图如下:
在这里附上数据库的表结构:
CREATE TABLE Student(
StuId VARCHAR(6) PRIMARY KEY,
StuName VARCHAR(10) NOT NULL,
MajorId INT NOT NULL,
Sex VARCHAR(2) NOT NULL DEFAULT '男',
Birthdate SMALLDATETIME NOT NULL,
Credit FLOAT,
Remark VARCHAR(200)
)
------------------------------------------------------------------------------------------------
程序代码如下:
Model层----------------------------------------------包含两个类------------------------
------------------表的实体类Student-----------------
public class Student
{
private string stuId;
public string StuId
{
get { return stuId; }
set { stuId = value; }
}
private string stuName;
public string StuName
{
get { return stuName; }
set { stuName = value; }
}
private int majorId;
public int MajorId
{
get { return majorId; }
set { majorId = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
private DateTime birthdate;
public DateTime Birthdate
{
get { return birthdate; }
set { birthdate = value; }
}
private float credit;
public float Credit
{
get { return credit; }
set { credit = value; }
}
private string remark;
public string Remark
{
get { return remark; }
set { remark = value; }
}
}
------------------Condition主要用于传递参数,这个类也可以定义在别的地方-------------------
public class Condition
{
public string paramName { get; set; }
public string paramValue { get; set; }
public ConditionOperate Operation { get; set; }
// 查询所用到的运算操作符
public enum ConditionOperate : byte
{
Equal, // 等于
NotEqual, // 不等于
Like, // 模糊查询
Lessthan, // 小于等于
GreaterThan // 大于
}
}
---------------------DAL层-----------------------------------------------------------------
------------------DBHelper类---------------------------------------------
public class DBHelper
{
private SqlConnection conn;
private SqlCommand cmd;
private SqlDataAdapter sda;
private DataSet ds;
public DBHelper()
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["key"].ConnectionString);
}
// 不带参数的查询
public DataSet GetResult(string sql, CommandType type)
{
cmd = new SqlCommand(sql, conn);
sda = new SqlDataAdapter(cmd);
conn.Close();
ds = new DataSet();
sda.Fill(ds, "student");
return ds;
}
// 带参数的查询
public DataSet GetResult(string sql, CommandType type, params SqlParameter[] paras)
{
cmd = new SqlCommand(sql, conn);
if (type == CommandType.StoredProcedure)
{
cmd.CommandType = CommandType.StoredProcedure;
}
cmd.Parameters.AddRange(paras);
sda = new SqlDataAdapter(cmd);
conn.Close();
ds = new DataSet();
sda.Fill(ds, "student");
return ds;
}
}
-----------------------------对Student表操作类
public class StudenDAL
{
public DataSet GetStudent(List<Condition> condition)
{
DataSet ds = new DataSet();
DBHelper db = new DBHelper();
string sql = "select * from student";
// 如果带查询语句带参数
if (condition.Count > 0)
{
sql += SqlString(condition);
ds = db.GetResult(sql, CommandType.Text, SqlParas(condition));
}
else
{
ds = db.GetResult(sql, CommandType.Text);
}
return ds;
}
----------------------以下两个可以写成一个类,以便如果有多张表时,可以实现代码的复用----------------------------------
// 获取查询参数
public SqlParameter[] SqlParas(List<Condition> cond)
{
List<SqlParameter> paras = new List<SqlParameter>();
for (int i = 0; i < cond.Count; i++)
{
SqlParameter para = new SqlParameter("@" + cond[i].paramName, cond[i].paramValue);
if (cond[i].Operation == Condition.ConditionOperate.Like)
{
para.SqlValue = "%" + cond[i].paramValue + "%";
}
paras.Add(para);
}
return paras.ToArray();
}
// 获取SQL查询语句的where子句
public string SqlString(List<Condition> cond)
{
string sqlWhere = string.Empty;
List<string> where = new List<string>();
// 数组元素的顺序应该与ConditionOperate枚举值顺序相同
string[] operateType = { " = ", " <> ", " Like ", " <= ", " >= " };
for (int i = 0; i < cond.Count; i++)
{
int index = (int)cond[i].Operation;
where.Add(string.Format("{0}" + operateType[index] + "{1}", cond[i].paramName, "@" + cond[i].paramName));
}
sqlWhere = " where " + string.Join(" and ", where.ToArray());
return sqlWhere;
}
}
------------------------------BLL层---------------------------
public class StudentBLL
{
private readonly StudenDAL stuDal = new StudenDAL();
public DataSet GetStudent(List<Condition> condition)
{
return stuDal.GetStudent(condition);
}
}
------------------------------UI层,查询按钮的单击事件-------------------------------------
protected void btnSearch_Click(object sender, EventArgs e)
{
Condition condition;
StudentBLL stu = new StudentBLL();
List<Condition> list = new List<Condition>();
if (txtId.Text!="")
{
condition = new Condition()
{
paramName = "stuId",
paramValue = txtId.Text,
Operation = Condition.ConditionOperate.Equal
};
list.Add(condition);
}
if (txtName.Text!="")
{
condition = new Condition()
{
paramName = "stuName",
paramValue = txtName.Text,
Operation = Condition.ConditionOperate.Equal
};
list.Add(condition);
}
if (txtSex.Text != "")
{
condition = new Condition()
{
paramName = "Sex",
paramValue = txtSex.Text,
Operation = Condition.ConditionOperate.Equal
};
list.Add(condition);
}
GridView1.DataSource = stu.GetStudent(list);
GridView1.DataBind();
}
转载于:https://www.cnblogs.com/ianunspace/p/3448524.html
ASP.NET三层架构之不确定查询参数个数的查询相关推荐
- asp.net三层架构连接Oracle 11g详解
asp.net三层架构连接Oracle 11g 连接Oracle时使用微软的Oracle连接组件: 一 DAL层 using System; using System.Collections.Gene ...
- .net mysql sqlhelper_「谢灿asp.net三层架构」5、DAL中公共类-SqlHelper类应该这样写
<谢灿asp.net三层架构>系列教程由小灿灿IT首发百度平台,希望对各位喜欢计算机的同学有所帮助!关注+分享+评论+点赞,是对我们最好的支持!有了您的支持,我们坚信我们会做得更好! 在C ...
- ASP.NET 三层架构技术 人力资源管理系统项目HR (深入WebServic
白菜价, 想要的联系QQ:867635458,非诚勿扰! ASP.NET MVC Linq 技术 企业级通用OA系统 全程开发 大型企业级别OA项目实战全新上线啦!本项目由小孔子讲师全程录制.小孔 ...
- ASP.NET 三层架构实现与数据库的连接验证登录
1.打开visual stdio->新建项目->Web->ASP.NET web应用程序->选择Empty 2.建好类库,右键选择解决方案"你的项目名"-& ...
- asp.net三层架构制作新闻管理_程序员蜕变为架构师必须要知道的「架构理论」...
架构目的和指标 架构目的: 架构设计的主要目的是为了解决软件系统复杂度带来的问题,是用最小的人力成本来满足需求的开发和响应需求的变化,用最小的运行成本来保障软件的运行.让软件达到"高内聚.松 ...
- ASP.NET三层架构全站开发的电商网站
这里写自定义目录标题 欢迎使用Markdown编辑器 新的改变 功能快捷键 合理的创建标题,有助于目录的生成 如何改变文本的样式 插入链接与图片 如何插入一段漂亮的代码片 生成一个适合你的列表 创建一 ...
- asp.net三层架构详解
一.数据库 /*==============================================================*/ /* DBMS name: Microsof ...
- asp.net三层架构应用详解【收录】
ASP.NET技术框架+脚本语言,对于不绑定数据的页面都用了HTML的静态控件.使用了最新的AJAX技术实现了无刷新的三级连动,通过继承IHttpHandler(处理器)类防止图片盗链,使用了大量用户 ...
- asp.net三层架构制作新闻管理_为什么使用PHP制作网站?
现在网站制作可以使用多种语言.为什么我们选择PHP进行网站制作? 北京东浩联创科技有限公司.是一家高端网站制作公司,在业界处于领先地位.本文东浩联创的小编告诉大家我们为什么使用PHP进行网站制作?它的 ...
最新文章
- 使用rpmbuild对ceph的源码包进行重新打包
- C++ Primer 读书笔记 (1)
- java中相同名字怎么声明,Java中,同一个类中的两个或两个以上的方法可以有同一个名字,只要他们的参数声明不同即可...
- 【自动驾驶/opencv】32.交通灯颜色提取的难点
- POJ1330(最近公共祖先)
- dicom文件转raw以及mhd时一些注意事项总结
- Jquery跨域调用(JSONP)遇到error问题的解决
- JavaFX之3 动画与事件处理
- 嵌入式单片机高级篇(一)Stm32F103电容触摸按键
- 222Beta多样性限制性排序CPCoA/CCA/RDA/LDA
- 解决可以上QQ,但是无法上网的问题
- CSS 新属性 《设置容器长宽比》
- 【电脑桌面不见了怎么办】
- 数据结构与算法(1)--二叉树
- 机器学习笔记(3.1)
- 数据结构更新中...
- 基于多智能体模型的街道步行空间量化研究
- CSS3: The missing manual 《css3秘笈》笔记+布局、设计优秀资源整理
- JavaScript 行间事件、提取行间事件
- 最新版安全狗(v4.0.2.665) 文件上传 绕过
热门文章
- Mysql存储引擎原理
- 关于pytorch中super的一点思考,结合代码
- c++ 错误: reference to local variable ‘...’ returned
- having and group by
- Linux /etc/group文件解析(超详细)
- 无忧计算机c语言二级题库,干货for计算机等级考试题库:二级C语言试题
- [BUUCTF-pwn]——picoctf_2018_are you root
- fork练习、从进程角度考虑堆区内存申请与释放的有关问题
- 新的C库Bionic的介绍
- Apache Commons:Commons-codec介绍