关于"bit"数据类型的问题

Wit*_*ors 0 c# sql asp.net

    public static bool CheckIfUserISbanned(Guid guid)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("SELECT Banned");
    sb.Append(" FROM dbo.Users");
    sb.Append(" WHERE UsersID=@UserID");
    object o;
    bool isBanned = false;
    string myConnectionString = AllQuestionsPresented.connectionString;
    using (SqlConnection conn = new SqlConnection(AllQuestionsPresented.connectionString))
    {
        SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
        conn.Open();
        cmd.Parameters.Add("@UserID", SqlDbType.UniqueIdentifier).Value = guid;
        o = cmd.ExecuteScalar();
    }

    isBanned = o == null ? false : true;//Problem here
    return isBanned;
}
Run Code Online (Sandbox Code Playgroud)

问题是该对象始终接收一个非null值.但是在Banned字段的Users表中,我将其类型设置为"Allow Nulls"...我可以看到有空值,但是没有null被重新读取.还有其他的...这使得"isBanned"参数成为真的...整个时间..为什么会发生,我怎么知道对象是什么时候是bool True.

Gra*_*ark 6

如果您的数据库查询NULL在SQL中返回,则会转换为DBNull.NET.因此,而不是测试null,测试DBNull.Value.