Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi to all,

Can you tell me how to insert Bit value in Mysql Database. I am giving below my C# Insert Query. Please Help me to fix problem.
C#
String str1 = "Insert  into ExamReview (Student_Name,QuestionPaperName,Question,Option1,Option2,Option3,Option4) values ('" + Session["User_Name"].ToString() + "','" + QuestionPaper + "','" + Question+ "','" + + "','" +  + "','" +  + "','" +  + "')";

In this Query, I want to insert Bit value 1 in Option1 And 0 In option2,Option3 and option4.

Thanks to all.
Posted
Updated 10-Jul-12 21:08pm
v2

This is my table
SQL
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| USER_ID           | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| USER_ABOUT_YOU    | varchar(255) | YES  |     | NULL    |                |
| USER_COUNTRY      | varchar(255) | YES  |     | NULL    |                |
| USER_GENDER       | varchar(255) | YES  |     | NULL    |                |
| USER_MAILING_LIST | bit(1)       | YES  |     | NULL    |                |
| USER_NAME         | varchar(255) | YES  |     | NULL    |                |
| USER_PASSWORD     | varchar(255) | YES  |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+


This is the query to insert data
INSERT INTO `user` (`USER_ID`, `USER_ABOUT_YOU`, `USER_COUNTRY`, `USER_GENDER`, `USER_MAILING_LIST`, `USER_NAME`, `USER_PASSWORD`) VALUES (2, 'omg', 'India', 'Female', b'1', 'test', 'test');
 
Share this answer
 
1) See MySQL reference about bit fields: http://dev.mysql.com/doc/refman/5.0/en/bit-field-literals.html[^]
2) Don't use string concatenation for building sql statement (you are exposing your app to SQLinjection), use parameters (sse: AddWithValue[^]) method.
 
Share this answer
 
Comments
Ajith_joseph 11-Jul-12 3:27am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900