首页 > 数据库技术 > 详细

SQL 判断是否用where或and

时间:2014-10-24 00:12:52      阅读:269      评论:0      收藏:0      [点我收藏+]

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;

SQL 判断是否用where或and

原文:http://www.cnblogs.com/wangweiabcd/p/4047063.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!