/// <summary>
/// 复合查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCheck_Click(object sender, EventArgs e)
{
//方法一
//StringBuilder sb = new StringBuilder();
//sb.Append("select * from T_Customers where 1=1\n");
//if (ckbName.Checked)
//{
// sb.Append("and Name like @name\n");
//}
//if (ckbPhone.Checked)
//{
// sb.Append("and Phone like @phone\n");
//}
//if (ckbCarNum.Checked)
//{
// sb.Append("and CarNum like @carnum\n");
//}
//MessageBox.Show(sb.ToString());
//方法二
List<string> listWhere = new List<string>();
if (ckbName.Checked)
{
listWhere.Add("Name like @name\n");
}
if (ckbPhone.Checked)
{
listWhere.Add("Phone like @phone\n");
}
if (ckbCarNum.Checked)
{
listWhere.Add("CarNum like @carnum\n");
}
string sql = "select * from T_Custumers\n";
if (listWhere.Count>0)
{
string sqlWhere = string.Join("and ", listWhere.ToArray());
sql += " where " + sqlWhere;
}
MessageBox.Show(sql);
}
本文转自蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366789,如需转载请自行联系原作者