- 1.声明泛型集合接收sql语句和参数,动态添加参数
- List<string> whereList=new List<string>();
- List<SqlParameter> paramsList = new List<SqlParameter>();
- if (cbSearchByName.IsChecked == true)
- {
- whereList.Add("Name=@Name");
- paramsList.Add(new SqlParameter("@Name",txtName.Text));
- }
- if (cbSearchByInDate.IsChecked == true)
- {
- whereList.Add("InDate>=@InDateStart and InDate<=@InDateEnd");
- paramsList.Add(new SqlParameter("@InDatestart",dpInDateStart.SelectedDate));
- paramsList.Add(new SqlParameter("@InDateEnd",dpInDateEnd.SelectedDate));
- }
- if (cbSearchByDepartment.IsChecked == true)
- {
- whereList.Add("DepartmentId=@DepartmentId");
- paramsList.Add(new SqlParameter("@departmentId",cmbDept.SelectedValue));
- }
- string whereSql = string.Join(" and ",whereList);//给sql语句添加条件
- string sql = "select * from T_Employee";
- if (whereSql.Length > 0)
- {
- sql = sql + " where " + whereSql;//拼凑sql条件语句
- }
- Employee[] result = new EmployeeDAL().Search(sql, paramsList);//添加参数
- datagrid.ItemsSource = result;
复制代码
|