Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Insert boolean value into database

I have problem inserting boolean value into database. I have simple structure:

struct
{
   string name;
   bool isStudent;
}

and I want to insert it into data base like this:

 dbCommand.CommandText = "INSERT INTO People (name, isStudent) VALUES ('" + people1.name + "', " + people1.isStudent + ")";
 dbCommand.ExecuteNonQuery();

but i throws exception:

SQLite error no such column: True

like image 658
user977386 Avatar asked Jan 19 '23 09:01

user977386


1 Answers

Try using:

dbCommand.CommandText = "INSERT INTO People (name, isStudent) VALUES ('" + people1.name + "', '" + people1.isStudent + "')";

Note that 'true' or 'false' will be quoted this way.

Or:

int val = isStudent ? 1 : 0;
dbCommand.CommandText = "INSERT INTO People (name, isStudent) VALUES ('" + people1.name + "', " + val + ")";

1 will be used for true values and 0 for false values.

like image 199
Pablo Santa Cruz Avatar answered Jan 23 '23 14:01

Pablo Santa Cruz