'L'运算符后缺少操作数

时间:2016-06-30 13:14:33

标签: c#

我的代码中出现以下错误:

  

'L'运算符后缺少操作数。

我调试了代码,发现它发生在下面一行:

DataRow[] documents = this.DataSet.Results_Documents.Select(String.Format("DocumentControlNumber = '{0}'", dcn), null, DataViewRowState.CurrentRows);

当dcn在其完整字符串中包含'时,就会发生这种情况。

这是什么原因?

例如:dcn: - Sheldon的Nat'l Bk

2 个答案:

答案 0 :(得分:6)

  

当dcn在其完整字符串中包含'时,就会发生这种情况。

烨;使用串联的常见问题;让我们从您的问题中使用示例dcn作为Nat'l Bk of Sheldon;查询现在是:

DocumentControlNumber = 'Nat'l Bk of Sheldon'

这是畸形的。由于这不允许正确的参数化,因此您需要" escape"价值;如果我不得不猜测,其中一个:

DocumentControlNumber = 'Nat''l Bk of Sheldon'

DocumentControlNumber = 'Nat\'l Bk of Sheldon'

(注意\ n C#需要转义,或使用逐字字符串文字)

要么可以通过string.Replace实现;例如dcn.Replace("'","''")(在string.Format的参数中)。

答案 1 :(得分:2)

Select字符串不再有效时,'要求字符串采用特定格式。您需要通过将每个'''

重新混合来转义引号
DataRow[] documents = this.DataSet.Results_Documents.Select(
                            String.Format("DocumentControlNumber = '{0}'"
                                         , dcn.ToString().Replace("'", "''")
                            ), null, DataViewRowState.CurrentRows);

如果dcnstring您不需要.ToString()