Read boolean values from DB?
Solution 1
If the type of delete_existing
is a sqlserver 'bit' type, you can do :
var i = reader.GetOrdinal("delete_existing"); // Get the field position
deleteExisting = reader.GetBoolean(i);
or (but it will crash if delete_existing
can be DBNull
)
deleteExisting = (bool)reader["delete_existing"];
or better, this onebelow is DBNull
proof and returns false if the column is DBNull
deleteExisting = reader["delete_existing"] as bool? ?? false;
Otherwise if the database type is int
:
deleteExisting = (reader["delete_existing"] as int? == 1) ? true : false;
or if it is a varchar
deleteExisting = (reader["delete_existing"] as string == "1") ? true : false;
Solution 2
Casting works: myVar = (bool)dataReader["myColumn"];
Solution 3
If you are using CASE in SELECT and want to use GetBoolean then use CAST to change the column to bit before reading.
For eg:
SELECT CAST((CASE WHEN [Condition] THEN 1 ELSE 0 END) as bit) FROM Table_Name
then you can use
reader.GetBoolean(0)
Solution 4
How about this?
deleteExisting = (reader["delete_existing"] as int?) == 1;
Boolean is probably the easist type to convert something to. Here's the 'Y', 'N' version:
deleteExisting = string.Equals(reader["delete_existing"] as string, "Y", StringComparision.OrdinalIgnoreCase);
Comments
-
Mr. Ant almost 2 years
In C#, using SqlDataReader, is there a way to read a boolean value from the DB?
while (reader.Read()) { destPath = reader["destination_path"].ToString(); destFile = reader["destination_file"].ToString(); createDir = reader["create_directory"].ToString(); deleteExisting = Convert.ToBoolean(reader["delete_existing"]); skipIFolderDate = reader["skipifolderdate"].ToString(); useTempFile = reader["useTempFile"].ToString(); password = reader["password"].ToString(); }
In the code above, delete_existing is always a 1 or 0 in the DB. I read on MSDN that Convert.ToBoolean() does not accept a 1 or 0 as valid input. It only accepts true or false. Is there an alternative way to convert a DB value to a bool? Or do I need to do this outside of the SqlDataReader?
Also, I can not change the DB values, so please no answers saying, "Change the DB values from 1's and 0's to true's and false's."
Thanks!
-
Mr. Ant about 13 years@Laurent Hmm... I would rather go with the third option because someone could add a column to the table and then, obviously, the app would break. However, can you explain what exactly is going on in the second option? I am confused by the "as bool? ?? false;" specifically the 3 ?'s...
-
Mr. Ant about 13 yearsthis statement returns an error. I assume it is because the GetBoolean function wants an Int passed in, and reader["delete_existing"] is not an int yet...
-
canon about 13 yearsThat works if the value returned is actually a bit. If someone lazily selected a 1 or 0 as an integer that cast will fail.
-
slandau about 13 yearsTry casting it? It should auto cast to an int I believe but you can try explicitly doing reader.GetBoolean((int)reader["delete_existing"]);
-
Larry about 13 years3rd solution: I'm trying to cast reader["delete_existing"] to a nullable bool (bool?). This way, the cast fails and returns null if the field contains DBNull.Value. Because I prefer to get false instead, I am using the ?? which transform a null to any value (ie: null ?? 45 = 45). I uses this a lot.
-
Larry about 13 yearsI'm sorry, in GetBoolean(x), x is not a value to convert to bool, but rather an integer that is the position of the field to read in the datareader.
-
Larry about 13 years2nd solution: I'm doing a direct cast to a bool. This one will throw an exception if the value is not a boolean type. For example, if it is DBNull.Value. That's why it should be used with not null bit database field. Just choose the one that fits your needs ^^ What is the database type of delete_existing btw ?
-
jgauffin about 13 yearsGetBoolean expects an ordinal = index of the column to get.
-
Mr. Ant about 13 years@Laurent What is the difference if I use a .ToString()? For example... deleteExisting = (reader["delete_existing"].ToString() == "1") ? true : false;
-
Larry about 13 yearsI'm afraid reader["delete_existing"].ToString() will return "True" or "False" instead of 1 or 0 if the underlying database type is bit. Otherwise, if it is a varchar or an int, deleteExisting = reader["delete_existing"].ToString() = "1"; is OK
-
Mr. Ant about 13 years@Laurent, Would you agree that as string is always a better option than .ToString()? Or is that debatable? Possibly another SO question...
-
Mr. Ant about 13 yearsstackoverflow.com/questions/2099900/… This answered my question above. Thanks for all your help!
-
Goldfish almost 7 yearsit will crash if delete_existing can be DBNull - gave me clue why my code was not working. Thanks
-
Larry about 3 yearsDid I really wrote
? true : false;
? This is totally redundant! XD