I am trying to use the Npgsql PostgreSQL client to accomplish two things:
- Avoid SQL injection, and
- Manage data containing the single quote '
I cannot see how to do either :(
PostrgeSQL version 9.1
In the below code, dx.chronic is of type bool? and cdesc of table dx may contain single quote, as "Tom's dog". Clearly, UpdateCmd, as written, will fail when Npgsql/PostgreSQL hits the single quote.
string sChronic = (dx.chronic == null) ? "null" : dx.chronic.ToString();
string UpdateCmd = "update dx "+
"set chronic = " + sChronic +
" where (trim(lower(cdesc)), trim(cicd9)) = "+
" ('"+dx.description.Trim().ToLower()+"','"+dx.icd9.Trim() +"');";
using (NpgsqlCommand command = new NpgsqlCommand(UpdateCmd, conn))
{
command.Parameters.Add(new NpgsqlParameter("value1", NpgsqlDbType.Text));
command.Parameters[0].Value = "Big Tom's Dog";
....... ? ? ? ? ? ? ? ? ? ? ? ? ? ...................
How is this done? Any help is most appreciated.
TIA