Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a Bit from SqlServer into c#

Tags:

c#

sql

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?

like image 961
Jorn Avatar asked Aug 26 '09 14:08

Jorn


2 Answers

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);
like image 69
LukeH Avatar answered Sep 21 '22 02:09

LukeH


Use the GetSqlBoolean method.

Update

Make sure you cast your return value as a boolean i.e.

var active = (bool)rdr.GetSqlBoolean(5);
like image 25
James Avatar answered Sep 22 '22 02:09

James