用单引号冲突C#的DataTable选择方法 [英] DataTable select method with single quote conflict C#

查看:476
本文介绍了用单引号冲突C#的DataTable选择方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我做了LINQ包含撇号的场下选择我最近发现,它使我的应用程序抛出异常。

I recently found when I do a LINQ select under a field that contains an apostrophe, it makes my application to throw an exception.

DataRow[] newDr = this.ds.Tables["TableName"].Select("Name = '" + drData["Name"].ToString() + "'");

如果drData [名称] =该企业的钱

If drData["Name"] = "The business's money"

我得到的异常:

I got an exception of "Syntax error: Missing operand after 'S' operator "

谁能告诉我如何保护它,并没有更换或删除它,好吗?

推荐答案

您必须将单引号逃脱2单引号,有可能是其他特殊字符,所以你必须照顾他们:

You have to escape the single quote to 2 single quotes, there may be other special characters so you have to take care of them:

DataRow[] newDr = this.ds.Tables["TableName"]
                         .Select(string.Format("Name ='{0}'",drData["Name"].ToString().Replace("'","''")));

在过滤器中的字​​符串转义特殊字符(使用选择<更多示例/ code>法):
行过滤字符串例子

More examples on escaping special characters in a filter string (used in Select method): Row filter string examples

这篇关于用单引号冲突C#的DataTable选择方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆