基于三层架构开发DAL层问题总结
1.面临多条件查询时sql脚本问题where 1 = 1的使用,可以简便sql脚本的连接
//说明当没有选择班级选项时classId = -1
public SqlDataReader GetReader(int classId, string studentId, string studentName)
{
List<SqlParameters> paramList = new List<SqlParameters>();//因为不知道参数有几个,所有不能用数组
string sql = "select StudentId, StudentName, Gender, ClassName from Students ";
sql += "inner join StudentClass on Students.ClassId = Student.ClassId ";
sql += "where 1 = 1 ";
if(studentId.Lenght != 0)
{
sql += "and StudentId = @StudentId ";
paramList.Add("@StudentId",studentId);
}
else
{
if(classId != -1)
{
sql += "and Students.ClassId = @ClassId ";
paramList.Add("@ClassId",classId);
}
if(studentName.Length != 0 && studentName != null)
{
sql += "and StudentName like @StudentName + '%' ";
paramList.Add("@StudentId",studentId);
}
}
}
来源:CSDN
作者:学习编程很简单,就是头容易凉
链接:https://blog.csdn.net/qq_36695087/article/details/103482764