.NET RowFilterでEvaluateExceptionが発生した場合のエスケープ

DataViewのRowFilterを利用してデータを抽出するとき
特殊文字をエスケープしないとEvaluateExceptionが発生します。

エスケープは文字を角かっこ[]で囲みます。

Dim findChar As String() = {vbTab, vbCr, vbLf, "~", "(", ")", "#", "\", "/", "=", ">", "<", "+", "-", "*", "%", "&", "|", "^", "'", "[", "]", """"}

Dim lstPattern As New List(Of String)
For Each s As String In findChar
    lstPattern.Add(System.Text.RegularExpressions.Regex.Escape(s))
Next
Dim sPattern As String = String.Join("|", lstPattern.ToArray)

Dim sInput As String = Me.TextBox1.Text
sInput = System.Text.RegularExpressions.Regex.Replace(sInput, sPattern, "[$&]")

Dim tbl As DataTable = ごにょごにょ
Dim viw As New DataView(tbl)
viw.RowFilter = "Column1 Like '%" & sInput & "%'"

0 件のコメント: