Recently I learned you can check if a value is null when setting the value for a SqlParameter
and if it's null set is a null. This was using the null-coalescing operator ??
which is very straight forward:
cmd.Parameters.Add(new SqlParameter("ID", ID.Text ?? (object)DBNull.Value);
Now can anyone explain this next example in more detail? Obviously, it's checking the string to see if it's empty but how does everything after the ?
operator work? Also what are the difference between ??
and ?
when it comes to performance?
cmd.Parameters.Add(new SqlParameter("ID", ID.Text == "" ? (object)DBNull.Value : ID.Text));