In my table, I use the bit type for IsTrue
column.
When I execute the select command:
SqlDataReader reader = command.ExecuteReader();
I'm not sure that what would the reader["isTrue"]
return ?
I tried doing the comparison
reader["isTrue"].ToString().Equals("0")
but it didn't work well. Can somebody tell me what I did incorrectly?
It returns a boolean value.
bool value = (bool)reader["IsTrue"];
If you know the index of the column in the result set, you can use:
bool value = reader.GetBoolean(index);
I like to do this with extension methods:
public static bool ToBool(this string theString)
{
bool result = false;
bool.TryParse(theString, out result);
return result;
}
And in your code you can simply do this:
if(reader["isTrue"].ToBool())
{
}
The ToBool extension method will return true/false in the following cases:
And it will never throw an exception when you execute it unless the "isTrue" column is not present in your result set, obviously.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.