views:

336

answers:

2

I'm writing a program in .NET2.0 and I need to escape the inputs before using them. Unfortunately the standard parameter method system does not fully work in the system I'm using. Using the ODBCCommand class I cannot place a ? parameter in the select part of the statement (which is required for the little bit of trickiness I'm doing) without getting an error, so I need to manually escape strings that may or may not contain a single quote ('). Any suggestions?

Edit- Example SQL:

As I would like it:

INSERT INTO TABLE_A (COLUMN_A, COLUMN_B)
SELECT (?, COLUMN_C)
FROM TABLE_B
WHERE COLUMN_D = ?

As it is:

INSERT INTO TABLE_A (COLUMN_A, COLUMN_B)
SELECT ('INPUT_VALUE_HERE', COLUMN_C)
FROM TABLE_B
WHERE COLUMN_D = ?

Edit: Sybase ASE is the DB driver, through ODBC

+2  A: 
Dim s As String = "Michael O'Flatley"
Dim escapedString as String = s.Replace("'", "''")
Patrick McDonald
+2  A: 

You can parse your string parameters with this extension function

public static string SqlEncode(string str)
{
    if (str == null) return String.Empty;
    return str.Replace("'","''");
}
Soni Ali
to be an extension function should it be public static string SqlEncode(this string str)?
Patrick McDonald
Fixed it for him.
Joel Coehoorn
Good idea, but if you look you'll see this .NET2.0 (no extension methods).
C. Ross
I did not make it an extension because he said .NET 2.0.
Soni Ali
Sorry Soni, you're right, I read extension in your answer and thought you meant to write an extension methos
Patrick McDonald