c# - 如何在 Microsoft SQL CE 中转义特殊字符

标签 c# sql database sqlcedatareader

NB: I am using Microsoft SQL Compact Edition 3.5

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

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);
 }
  • {0}=>用户
  • {1}=>显示名称
  • {2}=>图案

提前致谢

最佳答案

已发布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);

关于c# - 如何在 Microsoft SQL CE 中转义特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36833966/

相关文章:

c# - 为什么不是到处都是 "Try"方法?

c# float [] average 失去准确性

SQL Server 需要很长时间才能对几行执行更新查询

java - 准备好的语句有不同数量的 where 子句

python - 如何存储查询结果(使用 Python)

c# - C# 中是否有 System.IO.BufferedStream 的替代方案?

C# WPF - 打印目录中格式为 ".xml"的所有文件 - "could not find part of the path c#"

android - 在 Raw SQL Ormlite 中传递参数

mysql - 在Linux中调整mysql服务器

PHP如何将HTML字符串保存到数据库中