如何转义 Microsoft SQL CE 中的特殊字符

How to Escape special characters in Microsoft SQL CE

NB: I am using Microsoft SQL Compact Edition 3.5

我有一个 table of users.I 的显示名称作为用户输入,我需要查询显示名称与输入匹配的所有用户。

select TOP (1) * from users where display_name like 'Abby Parker'

这里'Abby parker'是输入

在正常情况下它工作正常。但问题是显示名称可以包含特殊字符

例如,显示名称可以是 "Abby Park#er" 或简单地 "%&^%&^%#%" .上面的查询在这种情况下失败了。我已经尝试过这里指定的解决方案

Escaping special characters in a SQL LIKE statement using sql parameters

这就是我在这里构建查询的方式

    var command = ceConnection.CreateCommand();
    command.CommandText = string.Format("select TOP (1) * from {0} where {1} like '[{2}]' ", tableName,fieldName, key);
 }

提前致谢

已发布 here,请尝试以下操作:

var command = ceConnection.CreateCommand();
command.CommandText = string.Format("select TOP (1) * from {0} where {1} like @key ", tableName,
                    fieldName);
command.Parameters.AddWithValue("@key", key);