如何处理DataTable.Select("")中的特殊字符

本文关键字:quot 特殊字符 DataTable 何处理 处理 Select | 更新日期: 2023-09-27 18:02:23

我正在从excel中检索数据,其中一列在excel中有'#',如"POLICY #"是excel中列的名称。

我的代码

string _policyNoColumn = dtFile.Columns[1].ToString();
string _policyNoRow = string.Empty;
foreach (DataRow _rows in dtFile.Rows)
{            
    _policyNoRow = _rows[1].ToString();
    DataRow[] _rowInFile = dtFile.Select(_policyNoColumn + " = '" + _policyNoRow + "'"); //Check on Excel
    if (_rowInFile.Count() == 2)
    {
        _lstInValid.Add(_policyNoRow); //Invalid
    }        
}

_policyNoColumn的值为" policy# "。这就是我得到的。

The expression contains invalid date constant '# = 'V0263680''.

和这是源错误

Line 501:        {            
Line 502:            _policyNoRow = _rows[1].ToString();
Line 503:            DataRow[] _rowInFile = dtFile.Select(_policyNoColumn + " = '" + _policyNoRow + "'"); //Check on Excel
Line 504:            if (_rowInFile.Count() == 2)
Line 505:            {

高亮部分为Line 503

如何处理DataTable.Select("")中的特殊字符

我得到答案了

我做了什么

string _policyNoColumn = "[" + dtFile.Columns[1].ToString() + "]";