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%"; 补充说明:即使在变量值前后,加上百分比%

MySQL:System.Data.Entity ,MySqlCommand, MySqlParameter and "LIKE" %的更多相关文章

  1. 报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败

    使用MVC和EF,在保存数据的时候报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败.有关详细信息, ...

  2. Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。

    <configSections> <!-- For more information on Entity Framework configuration, visit http:// ...

  3. 使用MVC和EF,在保存数据的时候报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。

    public UserInfo Create(UserInfo entity) { try { context.UserInfoes.Add(entity); context.SaveChanges( ...

  4. 报错:System.Data.Entity.Infrastructure.DbUpdateException 更新条目时出错

    背景 往数据库添加数据,前端验证通过的情况下,提交报错,程序停在了SaveChanges()这行,并报如上错误. 分析 猜想是提交的领域模型不符合数据库要求,但不知道具体哪里出错.网上查资料,有人发现 ...

  5. System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.InvalidOperationException: 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。

    2017/8/15 20:55:21 [AgentPayQuery_205506102_1BBBB]系统异常:System.Data.Entity.Core.EntityException: The ...

  6. Entity Framework优化一:引发了“System.Data.Entity.Core.EntityCommandExecutionException”类型的异常

    错误信息: “System.Data.Entity.Core.EntityCommandExecutionException”类型的异常在 EntityFramework.SqlServer.dll ...

  7. System.Data.Entity.Internal.AppConfig 类型初始值设定项引发异常

    在一开始时将connectionStrings 写在了configSections之上如下图一示,结果抛出:“System.Data.Entity.Internal.AppConfig”的类型初始值设 ...

  8. 使用EF6.0出现:CS0029 无法将类型“System.Data.Entity.Core.Objects.ObjectContext”隐式转换为“System.Data.Objects.ObjectContext”错误

    这是因为EF6.0重构了一些命名空间后,和VS原有的实体数据模型模板不一致了(ObjectContext context = ((IObjectContextAdapter)dataContext). ...

  9. 无法为具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer”。请确保使用限定程序集的名称且该程序集对运行的应用程序可用。有关详细信息,请参阅 http://go.m

    Windows服务中程序发布之后会如下错误: 无法为具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“Syste ...

随机推荐

  1. MongoDB基本操作(包括插入、修改、子节点排序等)

    一.基本操作 1.新增文章 db.article.insert({title:"今天天气很好",content:"我们一起去春游",_id:1}) 2.新增一条 ...

  2. Behavior Question - Most challenging project.

    介绍项目,challenging的地方 The most challenging project I have ever done was an online collaborative coding ...

  3. guxh的python笔记五:面向对象

    1,面向对象编程思想 类:一类具有相同属性的抽象 属性(静态属性):实例变量.类变量.私有属性 方法(动态属性):构造函数.析构函数(默认就有).函数.私有函数 对象/实例:类经过实例化后,就是对象/ ...

  4. Web版记账本开发记录(四)

    今天已经是是开发软件的第四天了,今天遇到了一些简单的小问题,虽然简单,但是自己仍旧不具备修改的能力, 自己尝试了各种办法仍旧没有修改成功,在收入表就状况百出,错误不断. 我决定明天还是静下心来好好地学 ...

  5. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1)C. Morse Code

    题意:给你n个01字符,每次问你前缀的所有本质不同的子串,由摩斯密码组成的方案数和. 题解:离线处理,把字符建sam,通过topo序来dp计算每个节点表示的子串方案数的和.统计答案时,把n个字符挨个匹 ...

  6. 查看局域网指定IP的电脑名

    nbtstat -a 192.168.0.139 节点 IP 址址: [192.168.0.140] 范围 ID: [] NetBIOS 远程计算机名称表 名称 类型 状态 ------------- ...

  7. spring cloud(三)服务提供与调用

    服务提供 我们假设服务提供者有一个hello方法,可以根据传入的参数,提供输出“hello xxx,this is first messge”的服务 1.pom包配置 创建一个springboot项目 ...

  8. redis特性,使用场景

    redis特性: 1.redis保存在内存中,读写速度快. 2.redis--持久化(断电数据不丢失:对数据的更新将异步保存到磁盘上). 3.redis数据结构丰富 4.redis功能丰富 5.简单( ...

  9. PS切图采坑

    博客记录我的各种采坑,有的坑很水,比如下面这个 谨记录自己的坑,以后作为入门笑料. 拿到设计师的psd设计图,理清事件逻辑,接下来就要切图啦.结果PS老是crash!检查了PS版本,也不低啊2017. ...

  10. css美化页面

    css美化页面 如果在我们一行文字中,想让某个文字凸显出来,使用span! 1.字体样式 font-style:字体的风格 italic normal font-weight:字体的粗细 normal ...