多条件查询

private void btnBegin_Click(object sender, EventArgs e)
{
//两个泛型liest,一个是保存条件的,另一个保存sqlparapeter
List<string> lisWhere = new List<string>();
List<SqlParameter> lisps = new List<SqlParameter>();

if (cbbQClasses.SelectedValue.ToString() != "-1")
{
lisWhere.Add("Cid=@Cid");
lisps.Add(new SqlParameter("@Cid", SqlDbType.Int) { Value = cbbQClasses.SelectedValue });
}
if (!string.IsNullOrEmpty(txtqName.Text.Trim()))
{
lisWhere.Add("name=@name");
lisps.Add(new SqlParameter("@name", SqlDbType.NVarChar) { Value = txtqName.Text.Trim() });
}
if (cbbqGender.SelectedIndex > 0)
{
lisWhere.Add("Gender=@gender");
lisps.Add(new SqlParameter("@gender", SqlDbType.NVarChar) { Value = cbbqGender.Text });
//下拉框在DropDownList的时候,SelectedText是取不到值的
}
string sql = "select * from student ";
if (lisWhere.Count > 0)
{
sql += "where "+ string.Join(" and ", lisWhere.ToArray());
}
//MessageBox.Show(sql);
List<Model.Student> lis = bll.GetStudents(sql, lisps.ToArray());
dgvMain.DataSource = lis;
}

posted @ 2014-04-24 21:26  小菜鸟飞飞  阅读(135)  评论(0编辑  收藏  举报