关于C#中MySQL语句带参数的模糊匹配问题
2008-11-26 00:49
543 查看
最近做一个网站,用的是MySQL数据库,但是当我用带参数的sql语句进行模糊查询时,发现MySQL没有识别我的参数中的内容。经过了多次实验,终于找到了答案,拿出来和大家分享。之前从网上找了好半天也没有找到答案呢,可能是我知道的论坛少之又少吧,O(∩_∩)O哈哈~
不多说了,详细如下:
public DataTable GetUserList(string strParam1,string strParam2,string strParam3,string strParam4)
{
StringBuilder sqlContent = new StringBuilder();
ArrayList paramList = new ArrayList();
sqlContent.Append(" SELECT ");
sqlContent.Append(" column1");
sqlContent.Append(" ,column2");
sqlContent.Append(" ,column3 ");
sqlContent.Append(" ,column4 ");
sqlContent.Append(" FROM ");
sqlContent.Append(" tab_temp ");
sqlContent.Append(" WHERE 1=1");
// 判断参数是否为空或""
if (!String.IsNullOrEmpty(strParam1))
{
sqlContent.Append(" AND column1 LIKE @param1 ");
// 添加参数
paramList.Add(new MySqlParameter("@param1", "%" + strParam1+ "%"));
}
if (!String.IsNullOrEmpty(strParam2))
{
sqlContent.Append(" AND column2 LIKE @param2 ");
paramList.Add(new MySqlParameter("@param2", "%" + strParam2 + "%"));
}
if (!String.IsNullOrEmpty(strParam3))
{
sqlContent.Append(" AND column3 LIKE @param3 ");
paramList.Add(new MySqlParameter("@param3", "%" + strParam3+ "%"));
}
if (!String.IsNullOrEmpty(strParam4))
{
sqlContent.Append(" AND column4 LIKE @param4 ");
paramList.Add(new MySqlParameter("@param4", "%" + strParam4+ "%"));
}
try
{
// 获取DB链接
dbConn.getConnection();
objDT = new DataTable();
// 调用DBUtil中查询方法
objDT = dbConn.executeQuery(sqlContent.ToString(), paramList);
}
catch (Exception e)
{
throw e;
}
finally
{
// 关闭DB链接
dbConn.closeConnection();
}
return objDT;
}
就是在动态添加参数这块出了问题,搞了我好半天的时间。
正确的写法:
sqlContent.Append(" AND column1 LIKE @param1 ");
// 添加参数
paramList.Add(new MySqlParameter("@param1", "%" + strParam1+ "%"));
错误的写法:
sqlContent.Append(" AND column1 LIKE '%@param1%' ");
// 添加参数
paramList.Add(new MySqlParameter("@param1", strParam1));
不多说了,详细如下:
public DataTable GetUserList(string strParam1,string strParam2,string strParam3,string strParam4)
{
StringBuilder sqlContent = new StringBuilder();
ArrayList paramList = new ArrayList();
sqlContent.Append(" SELECT ");
sqlContent.Append(" column1");
sqlContent.Append(" ,column2");
sqlContent.Append(" ,column3 ");
sqlContent.Append(" ,column4 ");
sqlContent.Append(" FROM ");
sqlContent.Append(" tab_temp ");
sqlContent.Append(" WHERE 1=1");
// 判断参数是否为空或""
if (!String.IsNullOrEmpty(strParam1))
{
sqlContent.Append(" AND column1 LIKE @param1 ");
// 添加参数
paramList.Add(new MySqlParameter("@param1", "%" + strParam1+ "%"));
}
if (!String.IsNullOrEmpty(strParam2))
{
sqlContent.Append(" AND column2 LIKE @param2 ");
paramList.Add(new MySqlParameter("@param2", "%" + strParam2 + "%"));
}
if (!String.IsNullOrEmpty(strParam3))
{
sqlContent.Append(" AND column3 LIKE @param3 ");
paramList.Add(new MySqlParameter("@param3", "%" + strParam3+ "%"));
}
if (!String.IsNullOrEmpty(strParam4))
{
sqlContent.Append(" AND column4 LIKE @param4 ");
paramList.Add(new MySqlParameter("@param4", "%" + strParam4+ "%"));
}
try
{
// 获取DB链接
dbConn.getConnection();
objDT = new DataTable();
// 调用DBUtil中查询方法
objDT = dbConn.executeQuery(sqlContent.ToString(), paramList);
}
catch (Exception e)
{
throw e;
}
finally
{
// 关闭DB链接
dbConn.closeConnection();
}
return objDT;
}
就是在动态添加参数这块出了问题,搞了我好半天的时间。
正确的写法:
sqlContent.Append(" AND column1 LIKE @param1 ");
// 添加参数
paramList.Add(new MySqlParameter("@param1", "%" + strParam1+ "%"));
错误的写法:
sqlContent.Append(" AND column1 LIKE '%@param1%' ");
// 添加参数
paramList.Add(new MySqlParameter("@param1", strParam1));
相关文章推荐
- C#中MySQL语句带参数的模糊匹配问题
- 关于c#执行mysql操作,传递参数的问题
- 关于mysql中的like语句带参数问题
- 关于c#执行mysql操作,传递参数的问题
- C#执行带参数的mysql语句
- 关于mysql里面写sql语句,或者直接在表里写汉字,都是乱码问题解决
- 代码中执行sql语句访问mysql,无法匹配中文字符的问题
- 谈关于mysql中文模糊查找问题
- MySql学习笔记4:模糊匹配语句
- c#执行带参数的MSSQL语句与MYSQL语句的不同
- mysql 存储过程参数的子参数问题,参数包含参数。。。ASP.ENT c# +mysql 5.5
- C#调用mysql,使用带参数mysql语句
- C#与MySQL的连接(关于图片存储问题)
- 关于C#函数对象参数传递的问题
- 关于C#函数对象参数传递的问题
- C语言学习4: 函数返回值与传入参数,关于函数值传递和类型隐性转换,变量不同的作用域,static变量,多文件编译例如两个C文件,显示函数调用语句跳转,递归,斐波那契数列,多文件编译相同变量的问题。
- 关于rs.Open sql,conn语句的参数问题
- 关于mysql 执行批量的sql 语句问题:&allowMultiQueries=true
- C#执行Oracle的Sql语句 参数顺序问题
- 关于mysql查询字符集不匹配问题的解决方法