MySQL:System.Data.Entity ,MySqlCommand, MySqlParameter and "LIKE" %
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 CodeJohn 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" %的更多相关文章
- 报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败
使用MVC和EF,在保存数据的时候报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败.有关详细信息, ...
- Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。
<configSections> <!-- For more information on Entity Framework configuration, visit http:// ...
- 使用MVC和EF,在保存数据的时候报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性。
public UserInfo Create(UserInfo entity) { try { context.UserInfoes.Add(entity); context.SaveChanges( ...
- 报错:System.Data.Entity.Infrastructure.DbUpdateException 更新条目时出错
背景 往数据库添加数据,前端验证通过的情况下,提交报错,程序停在了SaveChanges()这行,并报如上错误. 分析 猜想是提交的领域模型不符合数据库要求,但不知道具体哪里出错.网上查资料,有人发现 ...
- 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 ...
- Entity Framework优化一:引发了“System.Data.Entity.Core.EntityCommandExecutionException”类型的异常
错误信息: “System.Data.Entity.Core.EntityCommandExecutionException”类型的异常在 EntityFramework.SqlServer.dll ...
- System.Data.Entity.Internal.AppConfig 类型初始值设定项引发异常
在一开始时将connectionStrings 写在了configSections之上如下图一示,结果抛出:“System.Data.Entity.Internal.AppConfig”的类型初始值设 ...
- 使用EF6.0出现:CS0029 无法将类型“System.Data.Entity.Core.Objects.ObjectContext”隐式转换为“System.Data.Objects.ObjectContext”错误
这是因为EF6.0重构了一些命名空间后,和VS原有的实体数据模型模板不一致了(ObjectContext context = ((IObjectContextAdapter)dataContext). ...
- 无法为具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer”。请确保使用限定程序集的名称且该程序集对运行的应用程序可用。有关详细信息,请参阅 http://go.m
Windows服务中程序发布之后会如下错误: 无法为具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“Syste ...
随机推荐
- Median absolute deviation | Singular Value Decomposition奇异值分解 | cumulative sums |
Consider the data (1, 1, 2, 2, 4, 6, 9). It has a median value of 2. The absolute deviations about 2 ...
- fabric网络环境启动过程详解
这篇文章对fabric的网络环境启动过程进行讲解,也就是我们上节讲到的启动测试fabric网络环境时运行network_setup.sh这个文件的执行流程 fabric网络环境启动过程详解 上一节我们 ...
- python中mysql数据库的操作-sqlalchemy
MySQLdb支持python2.*,不支持3.* ,python3里面使用PyMySQL模块代替 python3里面如果有报错 django.core.exceptions.ImproperlyC ...
- weex中使用sass(失败)
在编辑器中可以正常编写不报错 但是在页面上不起作用 <style lang="scss" scoped></style> 第一步 安装依赖 npm i ...
- mysql安装和启动
1.在cmd的bin目录执行 mysqld --initialize-insecure程序会在动MySQL文件夹下创建data文件夹以及对应的文件2.bin目录下执行,mysqld --install ...
- vs2013打包安装程序
安装扩展包Visual Studio Installer VS2013下默认是没有Visual Studio Installer的,需要安装对应的扩展包: 下载地址:VS2013安装向导扩展包下载 安 ...
- python-类的约束,MD5,异常处理,日志
# # 项目经理 # class Base: # # 对子类进行了约束. 必须重写该方法 # # 以后上班了. 拿到公司代码之后. 发现了notImplementedError 继承他 直接重写他 # ...
- Redis常用命令与高级应用
附: 127.0.0.1:6379> set xiaofei 小飞 OK 127.0.0.1:6379> get xiaofei "\xe5\xb0\x8f\xe9\xa3\x9 ...
- 在Fiddler中添加自己所需的显示列
有时候我们想了解请求响应的时间.服务器IP等相关信息,Fiddler默认是没有显示的,这个时候就需要我们借助高级用法---自定义显示列, 其实主要就是操作这个文件Fiddler2\Scripts\Cu ...
- C语言gcc处理过程
gcc编译C文件一共四步,预处理(Preprocess),编译(Compilation),汇编(Assembly)和链接(Linking) 1. 预处理(Preprocess) 预处理是预处理中会展开 ...