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;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。