How can I change a bool to a 0 or a 1, can I cast it?

Just do value ? 1 : 0, it's that easy!


@Sean has given to you the natural fix to your problem, but, in my view, what you really need to do here is to refactor your App.Db.RunExecute to receive parameters, so you can write

public void UpdatePhrase(PHRASE phraseColumn, bool value, string phraseId) 
{
   sql = "UPDATE Phrase SET " + phraseColumn.Text() + " = @v WHERE PhraseId = @id";
   List<SqlParameter> prms = new List<SqlParameter>
   {
      new SqlParameter {ParameterName = "@v", SqlDbType = SqlDbType.Boolean, Value = value},
      new SqlParameter {ParameterName = "@id", SqlDbType = SqlDbType.NVarChar, Value = phraseId}
   };
   App.DB.RunExecute(sql, prms);
}

This will partially remove the Sql Injection problem (I say partially because that phraseColumn.Text() is still source of concerns if its value comes from the user input)

Now RunExecute should change to

void RunExecute(string sqlCommand, List<SqlParameter> prms = null)
{
     // usual code to open connection and create a command
     ......

     // If the caller passes a parameters list, add them to the command
     if(prms != null)
        cmd.Parameters.AddRange(prms.ToArray());

     // Now execute the command
     cmd.ExecuteNonQuery();
}

The change to RunExecute uses an optional argument, so your current code is not affected by the presence of the new argument but you will be able to write better Sql code from now on.

Tags:

C#