473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to store a bit field from SQL Server

Currently our SQL Server 2000 database table field is using char to store as
boolean, ie. "T" or "F".
Now we change the field from char to bit field. I am not sure how it has
impact on the C# code.

For example, the stored procedure returns
SELECT *
FROM employee

There is a field "ismale", "T" is male, "F" is female.
Now 1 is male, 0 is female.

I am not sure should I change the C# code or change the stored procedure?

1) If I leave the C# code unchanged
eg. to add an employee
- in C# code:
exec sp_add_employee 'alan', 'T'

- in SQL Server
so I need to change the stored procedure to test

create proc sp_add_emplyee
@name varchar(255),
@gender char
as
if @gender = 'T' then
insert into employee (empName, gender)
values (@name, 1)
else
insert into employee (empName, gender)
values (@name, 0)

But if I retrieve the data from database, I still need to modify the stored
procedure to return 1 or 0.
eg. in stored procedure
select empName, case gender.....1 else 0
from employee

2) if I leave the stored procedure unchanged, I need to modify the C# code,
if I have a record type to store the details of an employee, I need to
change the field type of the gender from char to 'bit'(or integer)
GUI binding also need to change, eg. check box, I use if it is "T" then
checked the box. Now I need to change the code if it is 1 then checked the
box.
Sep 15 '06 #1
5 26174
Alan T wrote:
Currently our SQL Server 2000 database table field is using char to store as
boolean, ie. "T" or "F".
Now we change the field from char to bit field. I am not sure how it has
impact on the C# code.

For example, the stored procedure returns
SELECT *
FROM employee

There is a field "ismale", "T" is male, "F" is female.
Now 1 is male, 0 is female.

I am not sure should I change the C# code or change the stored procedure?
Change the sp.
1) If I leave the C# code unchanged
eg. to add an employee
- in C# code:
exec sp_add_employee 'alan', 'T'

- in SQL Server
so I need to change the stored procedure to test

create proc sp_add_emplyee
@name varchar(255),
@gender char
@gender bit

when you create your command, if you use a parameterized query, you can
just add the @gender parameter and it will map bool straight to bit.
as
if @gender = 'T' then
insert into employee (empName, gender)
values (@name, 1)
else
insert into employee (empName, gender)
values (@name, 0)

But if I retrieve the data from database, I still need to modify the stored
procedure to return 1 or 0.
eg. in stored procedure
select empName, case gender.....1 else 0
from employee

2) if I leave the stored procedure unchanged, I need to modify the C# code,
if I have a record type to store the details of an employee, I need to
change the field type of the gender from char to 'bit'(or integer)
GUI binding also need to change, eg. check box, I use if it is "T" then
checked the box. Now I need to change the code if it is 1 then checked the
box.

Another question is why you have changed.
If it isn't broken and you don't have time to improve it then leave it.

JB

Sep 15 '06 #2
We think it is proper to change a boolean field from a char field to a bit
field.
Yes, we are using parameterizes query:

SqlParameter objParameter1 = new SqlParameter("@active", SqlDbType.Int);

objCommand.Parameters.Add(objParameter1);

objParameter1.Direction = ParameterDirection.Input;

objParameter1.Value = 1; // or 0 if "inactive"

Can I use like this?
"John B" <jb******@yahoo.comwrote in message
news:45**********@news.iprimus.com.au...
Alan T wrote:
>Currently our SQL Server 2000 database table field is using char to store
as boolean, ie. "T" or "F".
Now we change the field from char to bit field. I am not sure how it has
impact on the C# code.

For example, the stored procedure returns
SELECT *
FROM employee

There is a field "ismale", "T" is male, "F" is female.
Now 1 is male, 0 is female.

I am not sure should I change the C# code or change the stored procedure?

Change the sp.
>1) If I leave the C# code unchanged
eg. to add an employee
- in C# code:
exec sp_add_employee 'alan', 'T'

- in SQL Server
so I need to change the stored procedure to test

create proc sp_add_emplyee
@name varchar(255),
@gender char

@gender bit

when you create your command, if you use a parameterized query, you can
just add the @gender parameter and it will map bool straight to bit.
>as
if @gender = 'T' then
insert into employee (empName, gender)
values (@name, 1)
else
insert into employee (empName, gender)
values (@name, 0)

But if I retrieve the data from database, I still need to modify the
stored procedure to return 1 or 0.
eg. in stored procedure
select empName, case gender.....1 else 0
from employee

2) if I leave the stored procedure unchanged, I need to modify the C#
code,
if I have a record type to store the details of an employee, I need to
change the field type of the gender from char to 'bit'(or integer)
GUI binding also need to change, eg. check box, I use if it is "T" then
checked the box. Now I need to change the code if it is 1 then checked
the box.

Another question is why you have changed.
If it isn't broken and you don't have time to improve it then leave it.

JB

Sep 15 '06 #3
I would change the code if you can. The sql bit field maps to a boolean in
C#. you can set the parameter value in C# to be 1 or True.

Ciaran O'Donnell

"Alan T" wrote:
We think it is proper to change a boolean field from a char field to a bit
field.
Yes, we are using parameterizes query:

SqlParameter objParameter1 = new SqlParameter("@active", SqlDbType.Int);

objCommand.Parameters.Add(objParameter1);

objParameter1.Direction = ParameterDirection.Input;

objParameter1.Value = 1; // or 0 if "inactive"

Can I use like this?
"John B" <jb******@yahoo.comwrote in message
news:45**********@news.iprimus.com.au...
Alan T wrote:
Currently our SQL Server 2000 database table field is using char to store
as boolean, ie. "T" or "F".
Now we change the field from char to bit field. I am not sure how it has
impact on the C# code.

For example, the stored procedure returns
SELECT *
FROM employee

There is a field "ismale", "T" is male, "F" is female.
Now 1 is male, 0 is female.

I am not sure should I change the C# code or change the stored procedure?
Change the sp.
1) If I leave the C# code unchanged
eg. to add an employee
- in C# code:
exec sp_add_employee 'alan', 'T'

- in SQL Server
so I need to change the stored procedure to test

create proc sp_add_emplyee
@name varchar(255),
@gender char
@gender bit

when you create your command, if you use a parameterized query, you can
just add the @gender parameter and it will map bool straight to bit.
as
if @gender = 'T' then
insert into employee (empName, gender)
values (@name, 1)
else
insert into employee (empName, gender)
values (@name, 0)

But if I retrieve the data from database, I still need to modify the
stored procedure to return 1 or 0.
eg. in stored procedure
select empName, case gender.....1 else 0
from employee

2) if I leave the stored procedure unchanged, I need to modify the C#
code,
if I have a record type to store the details of an employee, I need to
change the field type of the gender from char to 'bit'(or integer)
GUI binding also need to change, eg. check box, I use if it is "T" then
checked the box. Now I need to change the code if it is 1 then checked
the box.
Another question is why you have changed.
If it isn't broken and you don't have time to improve it then leave it.

JB


Sep 15 '06 #4
Alan T wrote:
We think it is proper to change a boolean field from a char field to a bit
field.
Yes, we are using parameterizes query:

SqlParameter objParameter1 = new SqlParameter("@active", SqlDbType.Int);

objCommand.Parameters.Add(objParameter1);

objParameter1.Direction = ParameterDirection.Input;

objParameter1.Value = 1; // or 0 if "inactive"
This would work but you could more easily do:

objParameter1.Value = myClass.Active;

Then when you load from the database

myClass.Active = (bool)Reader["Active"];

(where reader is an idatareader and Active is the name of the column)

:)

JB

<...>
Sep 18 '06 #5
On Mon, 18 Sep 2006 16:30:07 +1000, John B <jb******@yahoo.comwrote:
>Alan T wrote:
>We think it is proper to change a boolean field from a char field to a bit
field.
Yes, we are using parameterizes query:

SqlParameter objParameter1 = new SqlParameter("@active", SqlDbType.Int);

objCommand.Parameters.Add(objParameter1);

objParameter1.Direction = ParameterDirection.Input;

objParameter1.Value = 1; // or 0 if "inactive"

This would work but you could more easily do:

objParameter1.Value = myClass.Active;

Then when you load from the database

myClass.Active = (bool)Reader["Active"];

(where reader is an idatareader and Active is the name of the column)

:)

JB

<...>
JB's solution will work very well indeed, but in the parameter declaration there
is a mistake. If Alan T is using MS SQL Server the declaration of the
parameter's type should be SqlDbType.Bit instead of SqlDbType.Int.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Sep 18 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Randell D. | last post by:
Folks, Perhaps someone can figure this out - this is 'the process of my script' I have a form whereby I can add multiple contacts to a single address. There is only one...
1
by: RayP | last post by:
I'd appreciate some help I'm having trying to run a cursor. First, some background. The Status field of all records on Table A needs changing from 1 to 0 where there is no corresponding record...
8
by: Jack | last post by:
Hi, Here is my problem: I am logging in to a page, where the page retrieves a record from a database. The text boxes are used in the display formto let the users update the fields, if they...
3
by: Noozer | last post by:
I have a Windows 2000 server. My ASP application allows users to upload a photo for display to other users. Unfortunately everyones photos are different sizes. I'd like to resize the photo to 150px...
3
by: Tony Johansson | last post by:
Hello! Assume you want to store field object that a chess board consist of. A chess board consist of 64 fields where each field is either white or black. Now to my question how should I...
12
by: SAL | last post by:
Hello, Is it possible to read a CSV from the Client, and bind my Datagrid to the data in the CSV file without uploading the file to the Server first? I have tried and in Debug mode on my...
6
by: omozali | last post by:
Hi all, How can I store field name into a variable and then access its properties? for example field text1 I'm trying something like this dim box as string box = "text1"
3
by: suresh_nsnguys | last post by:
Hi, Whether its possible to store the server data(jpg,flash and movie files) in the client hard disk.because i heard we can loaded in to RAM using javascript preloader .Its getting loaded in...
1
by: praveenkumarvpk | last post by:
Hi Expert...! I have a problem with recording a voice and storing to server. If we have a chance to make this possible please help me... My Requirement is to record a voice from...
1
by: sqlDummy | last post by:
I am a complete SQL noob, but have been left with a terrible task of rebuilding a report server where the previous admin was fired and left nothing for us. I have an ODBC connection that will...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.