1. boolean value in T-SQL
In T-SQL, bits are used to express boolean values.
(Bit is not necessarily stored as one character in SQL Server; in actual situations, bit is often stored as one byte and Multiple Digits .)
In SQL Server Browser (2008), true/false can be directly used as the input value of the bit column. SQL server automatically converts the input value true/false to 1/0.
2. Convert bit to bool in C #
In practice, I found that. Net 3.0/3.5 ADO can automatically convert the bit type to true/false without any additionalCode. At the same time, you can use sqlparameter to directly insert bool into SQL server without additional code.
The conversion between bit and bool is automatically completed in. Net 3.0/3.5 ADO.
Paste some code segments for my test:
Table Structure of the test:
Create Table bittest
(
Test bit,
)
C # code:
Retrieve BIT data
Sqlconnection connection = new sqlconnection ("Data Source = localhost; initial catalog = test; Integrated Security = sspi ;");
Sqldataadapter adapter = new sqldataadapter ("select * From bittest", connection );
Dataset DATA = new dataset ();
Adapter. Fill (data );
You only need to use tostring () to display data.
Console. writeline (data. Tables [0]. Rows [0] [0]. tostring ());
when inserting data, sqlparameter can automatically convert bool to bit
sqlcommand command = new sqlcommand ("insert into bittest ([test]) values (@ test) ", connection);
command. parameters. add (New sqlparameter ("@ test", false);
connection. open ();
command. executenonquery ();
connection. close ();