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

Nee*_*eel 1 c#

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

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

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

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

这是当dcn包含'在其完整字符串中时发生的.

这是什么原因?

例如:dcn: - 谢尔顿的Nat'l Bk

Mar*_*ell 6

这是当dcn包含'在其完整字符串中时发生的.

对; 使用串联的常见问题; 让我们使用的例子dcnNat'l Bk of Sheldon从你的问题; 查询现在是:

DocumentControlNumber = 'Nat'l Bk of Sheldon'
Run Code Online (Sandbox Code Playgroud)

这是畸形的.由于这不允许正确的参数化,您将需要"转义"该值; 如果我不得不猜测,其中一个:

DocumentControlNumber = 'Nat''l Bk of Sheldon'
Run Code Online (Sandbox Code Playgroud)

要么

DocumentControlNumber = 'Nat\'l Bk of Sheldon'
Run Code Online (Sandbox Code Playgroud)

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

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