I need to get a Bit from a sql server into c#. I tried differnt solutions like:
bool active = rdr.GetSqlBinary(5);
Int16 active = rdr.GetSqlBinary(5);
But can't find any way to get the Bit. Can someone give an example?
I need to get a Bit from a sql server into c#. I tried differnt solutions like:
bool active = rdr.GetSqlBinary(5);
Int16 active = rdr.GetSqlBinary(5);
But can't find any way to get the Bit. Can someone give an example?
Use the GetSqlBoolean method.
Update
Make sure you cast your return value as a boolean i.e.
var active = (bool)rdr.GetSqlBoolean(5);
Thx for the help, my final code became:
bool active = (bool)rdr.GetSqlBoolean(rdr.GetOrdinal("Active"));
On a sidenote, why can't I vote for an answer to be correct?
If you're certain that the column values will never be NULL
then the following will do the trick:
bool active = rdr.GetBoolean(rdr.GetOrdinal("Active"));
If it's possible that NULL
values might be returned:
int oActive = rdr.GetOrdinal("Active");
bool? active = rdr.IsDBNull(oActive) ? (bool?)null : rdr.GetBoolean(oActive);