简体   繁体   中英

How do I convert a boolean value to an int and save it to a MySQL table in C#?

I'm trying to convert a bool to an int and then save that into a MySQL table, but I get the wrong values:

Example:

Enabled = false;
Console.WriteLine(Enabled ? 1 : 0); //returns 0
DatabaseClient.AddParamWithValue("user_id", Session.GetUser().Id);
DatabaseClient.ExecuteQuery("UPDATE users SET friend_chat = " + (Enabled ? 1 : 0) + " WHERE Id = @user_id LIMIT 1;"); // returns empty

Enabled = true;
Console.WriteLine(Enabled ? 1 : 0); //returns 1
DatabaseClient.AddParamWithValue("user_id", Session.GetUser().Id);
DatabaseClient.ExecuteQuery("UPDATE users SET friend_chat = " + (Enabled ? 1 : 0) + " WHERE Id = @user_id LIMIT 1;"); // returns 0

I think you simply have the 2 mixed up. When Enabled is true, the first option will be chosen; ie 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM