Boolean value in T-SQL and how to convert bit to bool in C #

Source: Internet
Author: User

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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.