473,662 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 26234
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.Para meters.Add(objP arameter1);

objParameter1.D irection = ParameterDirect ion.Input;

objParameter1.V alue = 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.Para meters.Add(objP arameter1);

objParameter1.D irection = ParameterDirect ion.Input;

objParameter1.V alue = 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.Para meters.Add(objP arameter1);

objParameter1.D irection = ParameterDirect ion.Input;

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

objParameter1.V alue = 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.Par ameters.Add(obj Parameter1);

objParameter1. Direction = ParameterDirect ion.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.Acti ve = (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
3698
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 firstname/lastname/telephone box - when the user clicks the add button, I read the values from the form fields and record them into a hidden text input field. This part works because during debugging, I have converted the <input type=hidden> into a <input type=text>...
1
9476
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 on Table B. For each record that is changed the PWE, Staff Number and Status needs to be output to the screen. I have successfully run the SELECT statement but can't output anything to the screen. I've tried outputting the value of the table...
8
2598
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 desire to. The colorpreference and foodpreference fields are text fields while the FinalUpdate field is a yes/no field in Access Database.
3
1961
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 wide while maintaining the aspect ratio of the photo. Is there anything built into IIS/ASP that will allow me to do this? If not, is there a free DLL, etc. that I can add to the server? Thx!
3
7168
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 implement this putFieldAt below when I have field object that should be placed at position row i and kolumn j. So this putFieldAt is called 64 timmes where i has the value from 0 to 7 and j has value from 0 to
12
2886
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 workstation it works fine, but when I publish the page on our DEV server it doesn't fine the CSV file from the client. Has anyone done this before? If so, how do I do it? I'm new to ASP.net so
6
2048
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
2427
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 to RAM.but i want to store the same data in to hard disk. Basically if system got shut down,the data from RAM will be lost.If we store in the hard disk.the data will be there after user reboots. whether my concept is right, I have only...
1
1798
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 microphone(from client) and store that voice(.wav format) at server(webapps->audio folder) Please Please help me to do this.... Thank You....!
1
1532
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 accept sql queries, and the previous admin had sql server 2005 running and left us the ODBC System DSn in tact. what I want to do is run my query from the ODBC connection and store in server 2005. but again I have no clue...so any (relativly quick)...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8856
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8762
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.