通过带参数的Sql语句来实现模糊查询(多条件查询)

            #region 通过带参数的Sql语句来实现模糊查询(多条件查询)

            StringBuilder sb = new StringBuilder("select * from books");
List<string> listWheres = new List<string>();
List<SqlParameter> listParams = new List<SqlParameter>();
if (txtBookName.Text.Trim().Length > )
{
listWheres.Add(" bookName like @bkName ");
listParams.Add(new SqlParameter("@bkName", "%" + txtBookName.Text.Trim() + "%"));
} if (txtPubName.Text.Trim().Length > )
{
listWheres.Add(" PublishName like @pubName ");
listParams.Add(new SqlParameter("@pubName", "%" + txtPubName.Text.Trim() + "%"));
} if (txtPrice.Text.Trim().Length > )
{
listWheres.Add(" Price = @price");
listParams.Add(new SqlParameter("@price", double.Parse(txtPrice.Text.Trim())));
}
if (listWheres.Count > )
{
sb.Append(" where ");
string wheres = string.Join(" and ", listWheres.ToArray());
sb.Append(wheres);
}
MessageBox.Show(sb.ToString()); SqlParameter[] pms = listParams.ToArray(); #endregion
上一篇:SQL 中的多条件查询


下一篇:CenctOS6 and CenctOS7 多种姿势解决忘记密码