c# - 我们可以在c#中的datatable.select中添加参数吗

标签 c# datatable .net-2.0 c#-2.0

我想知道是否可以在 datatable.select(expression) 中添加参数。例如

string query="Name=@Name";          
//dt is comming from database.
dt.Select(query);

如何添加此参数@Name。我需要比较一个包含单引号的值,但在上述情况下它失败了。

提前致谢

最佳答案

您可以使用String.Format,您需要用两个单引号转义:

string query = string.Format("Name='{0}'", name.Replace(@"'", "''"));
var rows = dt.Select(query);

或者,如果你想使用Like:

string query = string.Format("Name LIKE '%{0}%'", name.Replace(@"'", "''"));

(请注意,DataTable 不易受到 sql 注入(inject)攻击,因为它是内存中的对象)

关于c# - 我们可以在c#中的datatable.select中添加参数吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18535076/

相关文章:

c# - 关于列表排序和委托(delegate)和 lambda 表达式 Func 的东西

c# - 阻止 System.Net.IPAddress 类中的混合 IPv6 格式

R flexdashboard 没有在标签集中显示我的所有数据表

R Shiny - 使用 DataTable 移动列名

c# - 模拟文件锁定时在 C# 中等待 File.Open

c# - 为什么编译器无法推断自定义集合的循环变量类型?

c# - 安装应用更新后,应用会丢失所有设置

c# - 合并两个没有重复值的数据表

.net - GDI+ 中随机发生的 AccessViolationException

winforms - "Enable the Visual Studio hosting"发布配置的进程编译选项