SqlParameter关于Like的传参数无效问题

时间:2022-05-18 20:51:52

正确的写法(简洁版)

private void GetHandleData(string strKeyWord1, string strKeyWord2, string strKeyWord3)
{
  string strSql = "select top 10 * from VW_Bookinfo where bcode like @strKeyWord1 and contacttel like @strKeyWord2 and spuname like @strKeyWord3 order by tdate desc";

  SqlParameter[] param = new SqlParameter[]
  {
    new SqlParameter("@strKeyWord1", SqlDbType.NVarChar,50),
    new SqlParameter("@strKeyWord2",SqlDbType.NVarChar,50),
    new SqlParameter("@strKeyWord3", SqlDbType.NVarChar,50)
  };
  param[0].Value = "%" + strKeyWord1 + "%";
  param[1].Value = "%" + strKeyWord2 + "%";
  param[2].Value = "%" + strKeyWord3 + "%";

  DataTable dt = ExecuteDataTableSql(strSql, param);

  DLView.DataSource = dt;
  DLView.DataBind();
}