Passing bool to a SQL statement

Z

Zatdam

Hi

I am relativly new to VS & C# so excuse me for this question

I am trying to pass a bool variable into a SQL statement but can't seem to
find the right syntax. I know to pass a string you use '"+strVar+"' but can't
seem to figure out bool.

(select * from tablet where active=chkActive.checkstate)
chkActive is on my windows form

Can someone point me in the right direction?

Many thanks for your help
 
J

Jeff Johnson

I am relativly new to VS & C# so excuse me for this question

I am trying to pass a bool variable into a SQL statement but can't seem to
find the right syntax. I know to pass a string you use '"+strVar+"' but
can't
seem to figure out bool.

(select * from tablet where active=chkActive.checkstate)
chkActive is on my windows form

Can someone point me in the right direction?

The real "right direction" would be to point you to documentation about "SQL
injection" and the use of "SqlCommand objects" instead of the dynamic string
building you're currently performing. I HIGHLY recommend you look into those
subjects. It'll show you how dangerous '" + strVar + "' is....

However, for the short term you can do something like this:

sql = "SELECT * FROM [SomeTable] WHERE SomeFlag = " + (chkActive.Checked ?
"1" : "0");
 
B

Ben Voigt [C++ MVP]

Jeff Johnson said:
The real "right direction" would be to point you to documentation about
"SQL injection" and the use of "SqlCommand objects" instead of the dynamic
string building you're currently performing. I HIGHLY recommend you look
into those subjects. It'll show you how dangerous '" + strVar + "' is....

Definitely a good point. On the other hand, only insertion of user-provided
strings is a problem, concatenating bool or numeric variables will never
cause special characters to be inserted and so is safe.
 
Z

Zatdam

Thanks guys. that worked a treat and will follow your advise and research
those subjects.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top