代码之家  ›  专栏  ›  技术社区  ›  Astaar

为什么我的DataTable总是返回“true/false”,但从不返回字符串?

  •  2
  • Astaar  · 技术社区  · 17 年前

    我试图使用一个由MySqlDataAdapter填充的数据表,其中包含一个博客条目的注释列表。出于某些原因,如果字段“anonymous”设置为“1”,则用户名字段为空,应替换为指定的字符串。

    我遇到的问题是,每当我试图获取字段的值时,我要么得到“true”,要么得到“false”。我的代码如下所示:

    DataTable modifiedComments = new DataTable();
    // The function GetCommentsById() returns a MySqlDataAdapter with the result of the query
    MySqlDataAdapter commentsContainer = Wb.Entry.GetCommentsById(imageId);
    commentsContainer.Fill(modifiedComments);
    commentsContainer.Dispose();
    
        foreach (DataRow row in modifiedComments.Rows)
            {
                string status;
                // This never returns true, so we always get into the else
                if (row["anonymous"] == "1")
                {
                        status = "You are anonymous";
                }
                else
                {
                        status = "You are not anonymous";
                }
            }
    
            viewImageCommentsRepeater.DataSource = modifiedComments;
            viewImageCommentsRepeater.DataBind();
    
    1 回复  |  直到 17 年前
        1
  •  5
  •   Philippe Leybaert    17 年前

    只需检查是否正确:

    if ((bool)row["anonymous"])
       ...
    
    推荐文章