SQL injection: isn't replace("'", "''") good enough?
I am wonder if replace "'" with "''" can prevent sql injection. I don't like it, but as the person in the original question, i have inherits a code base where that "bad practice" was used.
I read that post and am not sure if a sql injection for SQL Server is possible or not (seems a bit controversial answer).
So i would ask if someone can write a select ("escaped" in that way), that would finally fail agains a SQL Injection. If not possible i would assume it's safe.
---EDIT (added example extrapolated from real code, names are fictional due NDA but structure is the same) :
C# Code
string sql = $@"SELECT [FIELD1] ,[FIELD2], [FIELD3]
FROM [MY_TABLE]
WHERE [FIELD1] = '{UtilityBase.ChkString.(field1, "'")}'";
sql the is used here
using (System.Data.SqlClient.SqlDataAdapter xDtAdpt = new System.Data.SqlClient.SqlDataAdapter(StrSql, Conn))
{
RSDataSet = new System.Data.DataSet();
RSDataSet.EnforceConstraints = false;
xDtAdpt.Fill(RSDataSet);
RSDataSet.EnforceConstraints = true;
xDtAdpt.Dispose();
}
The check string is :
public static string ChkString(object xString, string xSeparator = "")
{
try
{
if (string.isNullOrEmpty(xString))
{
return "NULL";
}
else
{
return xSeparator + xString.ToString().Replace("'", "''") + xSeparator;
}
}
catch
{
return "";
}
}