我的代码中出现以下错误:
'L'运算符后缺少操作数。
我调试了代码,发现它发生在下面一行:
DataRow[] documents = this.DataSet.Results_Documents.Select(String.Format("DocumentControlNumber = '{0}'", dcn), null, DataViewRowState.CurrentRows);
当dcn在其完整字符串中包含'
时,就会发生这种情况。
这是什么原因?
例如:dcn: - Sheldon的Nat'l Bk
答案 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);
如果dcn
是string
您不需要.ToString()