The stored procedure returns a Boolean value and C # related processing,

Source: Internet
Author: User

The stored procedure returns a Boolean value and C # related processing,

Some time ago there was a problem where Boolean values were used between databases and programs.

For example, in SQL, do you want to determine whether the records are saved? Generally, you write:

DECLARE @ IsExists BIT = 0IF EXISTS (select top 1 1 FROM [dbo]. [SixSResponsiblePerson] WHERE [SixS_nbr] = @ SixS_nbr AND [UsersId] = @ UsersId) SET @ IsExists = 1 SELECT @ IsExistsView Code


Or you can follow the habits of Insus. NET:

Select case when exists (select top 1 1 FROM [dbo]. [SixSResponsiblePerson] WHERE [SixS_nbr] = @ SixS_nbr AND [UsersId] = @ UsersId) then cast (1 as bit) else cast (0 as bit) ENDView Code

 
In the database return value, the stored procedure only returns the data type bits, "1" and "0 ". Now we apply this stored procedure in the C # program:

In C #, when the data is converted to the bool value, only "0" is converted to false, and other values are converted to true.

In general, there is no problem with the above situation,

Let's take a look at the database design:

 

This field can be null, meaning three values are stored, true, false, and null.

When writing a stored procedure, you need to pay attention to it. We only need to judge if NULL is NULL or if 0 is false. Other judgments are true.

 

OK, the above stored procedure is also suitable for another situation, such as using tinyint.

 

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.