Add Regex string to database

OSVBNET 1,386 Reputation points
2022-08-13T02:51:13.077+00:00

Hello, I have a regex string (near 500 characters) so I created a table for it, since Short String is up to 255 chars I set it to Long String (Memo)
But I can't add it to the db:

OleDb.OleDbCommand("UPDATE MyData SET RegexString = '" + vRegexString + "'", MyConnection)

Syntax error (missing operator) in query expression ''^(([....

Well how can a regex get saved due to its varying chars?
Thank you for helping :)

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 114.7K Reputation points
    2022-08-13T03:14:45.1+00:00

    It seems that you are using the unrecommended and dangerous string concatenation. Try using parameterised queries, something like this:

    Dim cmd = new OleDb.OleDbCommand("UPDATE MyData SET RegexString = ?", MyConnection)  
    cmd.Parameters.AddWithValue( "s", vRegexString)  
    

    There are more approaches that are described in documentation.