#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 > 0) { listWheres.Add(" bookName like @bkName "); listParams.Add(new SqlParameter("@bkName", "%" + txtBookName.Text.Trim() + "%")); } if (txtPubName.Text.Trim().Length > 0) { listWheres.Add(" PublishName like @pubName "); listParams.Add(new SqlParameter("@pubName", "%" + txtPubName.Text.Trim() + "%")); } if (txtPrice.Text.Trim().Length > 0) { listWheres.Add(" Price = @price"); listParams.Add(new SqlParameter("@price", double.Parse(txtPrice.Text.Trim()))); } if (listWheres.Count > 0) { sb.Append(" where "); string wheres = string.Join(" and ", listWheres.ToArray()); sb.Append(wheres); } MessageBox.Show(sb.ToString()); SqlParameter[] pms = listParams.ToArray(); #endregion
时间: 2024-11-06 12:08:59