문제

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