16

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!

0

5 Answers 5

42

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;
Sign up to request clarification or add additional context in comments.

9 Comments

@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...
3rd 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.
2nd 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 ?
@Laurent What is the difference if I use a .ToString()? For example... deleteExisting = (reader["delete_existing"].ToString() == "1") ? true : false;
stackoverflow.com/questions/2099900/… This answered my question above. Thanks for all your help!
|
3

Casting works: myVar = (bool)dataReader["myColumn"];

1 Comment

That works if the value returned is actually a bit. If someone lazily selected a 1 or 0 as an integer that cast will fail.
1

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

1

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)

Comments

-1
deleteExisting = reader.GetBoolean(reader["delete_existing"]);

4 Comments

this 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...
Try casting it? It should auto cast to an int I believe but you can try explicitly doing reader.GetBoolean((int)reader["delete_existing"]);
I'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.
GetBoolean expects an ordinal = index of the column to get.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.