c#DataTable选择不使用特殊字符# [英] c# DataTable select not working with special characters #
问题描述
我有一个数据选择,如:
I have a datatable select like:
productData.Select("Name = 'AAA BBB # CCC'");
我知道条目在那里,因为#字符不起作用。我已经尝试使用[]转义:
I know the entry is there, it just doesn't work because of the # character. I have tried escaping with [] like:
productData.Select("Name = 'AAA BBB [#] CCC'");
但它仍然不起作用。我知道单引号我把它们双倍变成'成'。但是我需要关心哪些其他角色,以及如何使这种情况发挥作用。
but it still doesn't work. I know for single quotes I double them so ' becomes ''. But what other characters do I need to be concerned with and how to get this case to work.
推荐答案
你绝对是有使用这样的数据表?我一直对DataTable中的基于文本的查询感到非常紧张,正是由于这个原因。
Do you absolutely have to use DataTables like this? I've always been incredibly nervous of the text-based querying in DataTable for precisely this reason.
如果可能,建议您开始使用LINQ。您可以使用 DataTable
,例如
If at all possible, I suggest you start using LINQ. You can do that with DataTable
already, e.g.
var query = products.AsEnumerable()
.Where(row => row.Field<string>("Name") == "AAA BBB # CCC");
这样你就不需要担心转义等等。如果使用强类型的数据集它变得更加简单,因为您可以直接引用属性而不是使用字符串名称。
That way you don't need to worry about escaping etc. If you use a strongly-typed dataset it becomes even simpler, as you can refer to properties directly instead of using string names.
这篇关于c#DataTable选择不使用特殊字符#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!