473,396 Members | 2,147 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,396 software developers and data experts.

"Bit" field in SQL Server

I am in the process of upgrading my application db from Access to SQL
Server.

With the Access backend, I had queries such as "UPDATE table SET
fieldName = True". SQL Server doesnt seem to like TRUE as a bool value,
instead it requires 0 and 1.

My queries are based on user input, so if a checkbox is checked, I
create my query as follows:

"UPDATE table SET fieldName = " + checkbox.selected

which translates to

"UPDATE table SET fieldName = True"

Do I have to rewrite all my queries to conver the "True" to 0/1? Or is
there an easier way?

Bijoy

Nov 19 '05 #1
4 8058
If you use the SqlClient namespace (most likely works with OleDb as well),
you can throw a bool at a bit field and ADO.NET will translate for you. You
do not have to create code to turn true into 1 and false into 0.

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

"bijoy" wrote:
I am in the process of upgrading my application db from Access to SQL
Server.

With the Access backend, I had queries such as "UPDATE table SET
fieldName = True". SQL Server doesnt seem to like TRUE as a bool value,
instead it requires 0 and 1.

My queries are based on user input, so if a checkbox is checked, I
create my query as follows:

"UPDATE table SET fieldName = " + checkbox.selected

which translates to

"UPDATE table SET fieldName = True"

Do I have to rewrite all my queries to conver the "True" to 0/1? Or is
there an easier way?

Bijoy

Nov 19 '05 #2
You should use parameterized queries. The way you do it, is not immune
to SQL injection (even though the input may be very limited in this
case). It is _always_ a good idea to use parameterized queries, because
some important things are taken into account, so you do not have to
bother with it yourself.

Your code will then look something like the following instead:

XXXCommand command = new XXXCommand("UPDATE table SET fieldName =
'@fieldName'", connection);
command.Parameters.Add(new XXXParameter("@fieldName",
checkbox.Checked));

----
- Wilco Bauwer
Blog & Custom Controls @ http://wilcoding.xs4all.nl

Nov 19 '05 #3
I second what Cowboy said, but also, you should use parameterized queries,
so your update statement should look like:

command.commandText = "Update Table set fieldName = @Value"
command.Parameters.Add("@Value", SqlDbType.Bit).Value = checkbox.selected

this will (a) help protect against SQL Injection (b) make it easier for you
to move to sprocs (if you decide to do so).

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/index.aspx - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"bijoy" <b_*****@yahoo.ca> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I am in the process of upgrading my application db from Access to SQL
Server.

With the Access backend, I had queries such as "UPDATE table SET
fieldName = True". SQL Server doesnt seem to like TRUE as a bool value,
instead it requires 0 and 1.

My queries are based on user input, so if a checkbox is checked, I
create my query as follows:

"UPDATE table SET fieldName = " + checkbox.selected

which translates to

"UPDATE table SET fieldName = True"

Do I have to rewrite all my queries to conver the "True" to 0/1? Or is
there an easier way?

Bijoy

Nov 19 '05 #4
I am using SqlHelper ( the MS Application Data Access Block). I believe
it uses SQL Client. But I still have this prob.

Nov 19 '05 #5

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

Similar topics

5
by: Taylor Howell | last post by:
Hello all, I have a delima. I have 8 5bit numbers that I need to pack into one (or more) variables. They then must be written (exactly 40bits (5Bytes)) to a file and have the ability to be put...
2
by: Mike | last post by:
I need to determine where the "bit data" flag (as applied on a CHAR and other columns) is stored in the system tables. I'm trying to add data mappings to our product (MyGeneration) so if you ask me...
2
by: jian | last post by:
Hi, I am looking at netinet/ip.h in linux code, and the declaration: unsigned int ip_hl:4 make me confused. I can understand "unsigned int ip_hl", but, what does ":4" here mean? Very...
40
by: aku | last post by:
I'm looking for the absolute fastest way to count the nr of bits that are set to "1" in a string. Presumably I then first need the fastest way to do this in a byte. I think this is it, but...
22
by: campbellbrian2001 | last post by:
Thanks in Advance! ... I have two textboxes: 1 is visible (and gets its value based on the invisible textbox and displays either "Male" or "Female", and needs to display either male of female based...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
2
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to...
18
by: Stephan Beal | last post by:
Hi, all! Before i ask my question, i want to clarify that my question is not about the code i will show, but about what the C Standard says should happen. A week or so ago it occurred to me...
1
rohypnol
by: rohypnol | last post by:
Do any firewalls actually block TCP packets with the "Evil bet" set or is that just a myth? Or is it actually used, but for something completely different?
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.