There were similar questions, but the answers were not what I was looking for. I want to insert a NULL value in a SQL Server database if the link is NULL or the value is not assigned yet. I'm currently testing null and looks like
String testString = null; if (testString == null) { command.Parameters.AddParameter(new SqlParameter("@column", DBNull.Value); } else { command.Parameters.AddParameter(new SqlParameter("@column", testString); }
It looks and seems incredibly awkward to me. I have quite a few values ββthat I insert into the database, and there are a lot of checking them all, as above. Does .Net do this? I thought maybe if I used a string as opposed to a String, but that also does not work. Looking around, I found articles that talk about using Nullable types.
System.Nullable<T> variable
It looks like primitives, int ?, char? double? and bool ?. So this might work for those but about strings? I missed something. What types should I use for primitive values ββand for string values, so I do not need to check the values ββmany times before inserting them.
EDIT: Before getting too many answers about triple operators. I like them, but not in this context. It makes no sense for me to test this value and have all this additional logic when such a thing could be implemented below in the .Net structure, and if I knew what types to give, then it would receive it for free.
Edit: Okay, so the guys are helping me formulate my attack plan. I will use Nullable for my primitives (int ?, double? And so on), And for my strings I will use String, but ??? test. This makes things less verbose. Is there something that I am missing here, for example, that could lose semantics?
uriDium
source share