2014-02-08 59 views
3

我有以下代碼:「埃裏克」DataTable表達式不能解釋標記'!'

//myDataTable has the following collumns: UserName, Birthday and email. 
string name = "eric!"; 
string expression = "UserName = " + name; 
DataRow[] result = myDataTable.Select(expression); 

我要選擇的名稱都行。
「!」給我以下錯誤:

Cannot interpret token "!".

我該如何選擇帶有這些標記的所有行?
(我真的需要「!」的表達,因爲我提取.sql文件的用戶名)

回答

4

您應該在''之間使用您的name。喜歡;

string name = "'eric!'"; 

沒有單引號,你DataTable.Select方法認爲!操作並沒有在DataColumn.Expression property允許作爲一個有效的運營商。

文檔;

User-Defined Values

User-defined values may be used within expressions to be compared with column values. String values should be enclosed within single quotation marks.

2

你缺少引號(' ')圍繞你的價值

string name = "eric!"; 
string expression = "UserName = '" + name+'"; 
DataRow[] result = myDataTable.Select(expression); 

當你寫濾波器算不引用和!,它會考慮!不是運營商這就是爲什麼它會給出錯誤。