关于C#中MySQL语句带参数的模糊匹配问题

    最近做一个网站,用的是MySQL数据库,但是当我用带参数的sql语句进行模糊查询时,发现MySQL没有识别我的参数中的内容。经过了多次实验,终于找到了答案,拿出来和大家分享。之前从网上找了好半天也没有找到答案呢,可能是我知道的论坛少之又少吧,O(∩_∩)O哈哈~

不多说了,详细如下:

 

  1. public DataTable GetUserList(string strParam1,string strParam2,string strParam3,string strParam4)
  2. {
  3.             StringBuilder sqlContent = new StringBuilder();
  4.             ArrayList paramList = new ArrayList();
  5.             sqlContent.Append(" SELECT ");
  6.             sqlContent.Append("     column1");
  7.             sqlContent.Append("     ,column2");
  8.             sqlContent.Append("     ,column3 ");
  9.             sqlContent.Append("     ,column4 ");
  10.             sqlContent.Append(" FROM ");
  11.             sqlContent.Append("     tab_temp ");
  12.             sqlContent.Append(" WHERE 1=1");
  13.             // 判断参数是否为空或"" 
  14.             if (!String.IsNullOrEmpty(strParam1))
  15.             {
  16.                 sqlContent.Append(" AND column1 LIKE @param1 ");
  17.                 // 添加参数 
  18.                 paramList.Add(new MySqlParameter("@param1""%" + strParam1+ "%"));
  19.             }
  20.             if (!String.IsNullOrEmpty(strParam2))
  21.             {
  22.                 sqlContent.Append(" AND column2 LIKE @param2 ");
  23.                 paramList.Add(new MySqlParameter("@param2""%" + strParam2 + "%"));
  24.             }
  25.             if (!String.IsNullOrEmpty(strParam3))
  26.             {
  27.                 sqlContent.Append(" AND column3 LIKE @param3 ");
  28.                 paramList.Add(new MySqlParameter("@param3""%" + strParam3+ "%"));
  29.             }
  30.             if (!String.IsNullOrEmpty(strParam4))
  31.             {
  32.                 sqlContent.Append(" AND column4 LIKE @param4 ");
  33.                 paramList.Add(new MySqlParameter("@param4""%" + strParam4+ "%"));
  34.             }
  35.             try
  36.             {
  37.                 // 获取DB链接 
  38.                 dbConn.getConnection();
  39.                 objDT = new DataTable();
  40.                 // 调用DBUtil中查询方法 
  41.                 objDT = dbConn.executeQuery(sqlContent.ToString(), paramList);
  42.             }
  43.             catch (Exception e)
  44.             {
  45.                 throw e;
  46.             }
  47.             finally
  48.             {
  49.                 // 关闭DB链接 
  50.                 dbConn.closeConnection();
  51.             }
  52.             return objDT;
  53.         }

就是在动态添加参数这块出了问题,搞了我好半天的时间。

正确的写法:

  1.  sqlContent.Append(" AND column1 LIKE @param1 ");
    •  // 添加参数 
    •  paramList.Add(new MySqlParameter("@param1""%" + strParam1+ "%"));

错误的写法:

  1. sqlContent.Append(" AND column1 LIKE '%@param1%' ");
  2. // 添加参数
  3.  paramList.Add(new MySqlParameter("@param1",  strParam1));
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值