SQL 判断是否用where或and
StringBuilder sql = new StringBuilder();
sql.Append("select DeskId,DeskName,DeskNamePinYin,DeskNum from DeskInfo ");
bool flag = false;
List<SqlParameter> pms = new List<SqlParameter>();
#region 判断是否用where或and
if (!string.IsNullOrEmpty(tbName.Text.Trim()))
{
if (!flag)
{
sql.Append(" where DeskName like @DeskName ");
flag = true;
}
else
{
sql.Append(" and DeskName like @DeskName ");
}
pms.Add(new SqlParameter("@DeskName", "%" + tbName.Text.Trim() + "%"));
}
if (!string.IsNullOrEmpty(tbPinYin.Text.Trim()))
{
if (!flag)
{
sql.Append(" where DeskNamePinYin like @DeskNamePinYin ");
flag = true;
}
else
{
sql.Append(" and DeskNamePinYin like @DeskNamePinYin ");
}
pms.Add(new SqlParameter("@DeskNamePinYin", "%" + tbPinYin.Text.Trim() + "%"));
}
if (!string.IsNullOrEmpty(tbNum.Text.Trim()))
{
if (!flag)
{
sql.Append(" where DeskNum like @DeskNum ");
flag = true;
}
else
{
sql.Append(" and DeskNum like @DeskNum ");
}
pms.Add(new SqlParameter("@DeskNum", "%" + tbNum.Text.Trim() + "%"));
}
#endregion
DataTable dt = SqlHelper.ExecuteTable(sql.ToString(), pms.ToArray());
List<DeskInfo> list = ChangeDtToList(dt);
dgvDeskInfo.DataSource = list;