Introduction

Using GetSqlStringCommand with a text comparative, with LIKE, in ADO.NET and the MySQLParamenter gets you different result between executing by hand the command in a MySQL client and executing it through ADO.NET.

Background

This occurs when you write a command like this "SELECT * FROM users WHERE name LIKE '%John%'", this will return:

Collapse | Copy Code
John Frank
Johnny Philips
H. F. John

But for ADO.NET if you set a var, like "@name" and update the command like this "SELECT * FROM users WHERE name LIKE '%@name%'", ADO.NET treats it as the string '@name' you will return 0 result, because no exists any user with the name @name or the email @name, but yes someone with an email of the domain "name.com", like alberto@name.com, but this is a casualty and not, what we expect.

So you need to remove the simple quota and set the value appending and preceding with "%".

Using the code

//错误写法
MySQLCommand cmd = oldDb.GetSqlStringCommand(CommandType.Text,"SELECT * FROM users WHERE name LIKE '%@name%'");
MySQLParameter nameParameter= cmd.CreateParameter();
nameParameter.DbType = DbType.String;
nameParameter.ParameterName = "@name";
nameParameter.Value = "John"; //正确写法
MySQLCommand cmd = oldDb.GetSqlStringCommand(CommandType.Text,"SELECT * FROM users WHERE name LIKE @name");
MySQLParameter nameParameter= cmd.CreateParameter();
nameParameter.DbType = DbType.String;
nameParameter.ParameterName = "@searchText"
nameParameter.Value = "%John%"; 补充说明:即使在变量值前后,加上百分比%

最新文章

  1. Oracle索引梳理系列(五)- Oracle索引种类之表簇索引(cluster index)
  2. C#字符串的方法
  3. C#中获取当前时间:System.DateTime.Now.ToString()用法
  4. Java对象的序列化和反序列化
  5. Sqli-LABS通关笔录-17-审计SQL注入
  6. yum源的修改
  7. C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\Team Foundation\4.0\Cache\VersionControl.config is not valid and cannot be loaded.
  8. 初学java,遇到的陌生词语(1)
  9. 天津Uber优步司机奖励政策(1月25日~1月31日)
  10. 转:15 Best Responsive Web Design Testing Tools
  11. Serializable在C#中的作用——.net中的对象序列化
  12. Java的跨平台原理
  13. poj1797 Heavy Transportation Dijkstra算法的简单应用
  14. 电梯调度二——曹玉松&&蔡迎盈
  15. 2018-2019-2 网络对抗技术 20165317 Exp4 恶意代码分析
  16. ABP 设置默认为中文
  17. geopandas overlay 函数报错问题解决方案
  18. 【Java】-NO.16.EBook.4.Java.1.004-【疯狂Java讲义第3版 李刚】- 内部类
  19. sql server web管理软件
  20. 20155325 信息安全技术 实验二 Windows口令破解

热门文章

  1. js中defer实现等文档加载完在执行脚本
  2. linux中执行shell命令的几种常用方法
  3. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1)C. Morse Code
  4. python -- 字典 集合
  5. 微服务-封装-docker by daysn大雄
  6. c语言:开平方根sqrt程序01
  7. LNMP(二)
  8. html实现滚动播报(原生JS实现)
  9. URL和URI(简单介绍)
  10. 学号 20175223 《Java程序设计》第2周学习总结