I'm retrieving data from a MySQL table and displaying it on a webpage using MySqlDataReader and .NET (C#), with no trouble retrieving simple text data, but I'm having trouble retrieving data from a boolean column, as the possible values are either "1" or "null" and GetBoolean() doesn't work in this instance.

I'm using the GetString() and GetDateTime() methods for the aforementioned columns (e.g. myReader.GetString("name") & myReader.GetDateTime("startDate")), is there a similar method for retrieving the boolean values when they are label as either "1" or "null"?

有帮助吗?

解决方案

Use IsDBNull() in your code:

if(myReader.IsDBNull("BooleanColumnName") == true)
{
    // column contains a null value
    // handle null as you see fit
}
else
{
    var columnValue = myReader.GetBoolean("BooleanColumnName");
    if(columnValue == true)
    {
        // column value is "true"
    }
    else
    {
        // column value is "false"
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top