我有一个名为call的数据库表,其中列call_time,location,emergency_type,并且有三种类型的紧急情况:医务人员,警察和消防员。在Windows窗体中,我创建了CheckBoxes的“护理人员”,“警察”,“消防员”,我想检索满足用户选择的所有表格列。检索符合选定条件的数据的最佳做法
我创建了一个功能:
public static DataTable GetHistory(DateTime from, DateTime to, bool paramedics, bool police, bool firefighters)
{
string select =
"SELECT call_time, location, emergency_type where call_time between @from AND @to AND";
if(paramedics)
{
select += " emergency_type = 'paramedics' ";
}
if(paramedics && police)
{
select +=" emergency_type = 'paramedics' OR emergency_type = 'police';
}
...
}
此代码然而看起来非常脏,因为如果有30个各种紧急会有30!在编写所有if语句之前,我会变老。
如果您共享您检索符合所选搜索条件,如果有,你可以chosse很多选择数据的做法我将不胜感激。
谢谢!
顺便说一句,如果语句运行了一个又一个,所以如果医护人员和警察都是真的,你wouold用字符串” ...... emergency_type最终=‘救护’emergency_type =‘救护’OR emergency_type =‘警察’ “ – Ady 2008-11-01 08:58:17
这只是一个方法示例,而不是工作版本。 – 2008-11-01 20:02:22