【问题标题】:Why does my DataTable always returns "true/false" but never a string?为什么我的 DataTable 总是返回“true/false”但从不返回字符串?
【发布时间】:2010-11-10 02:46:26
【问题描述】:

我正在尝试使用由 MySqlDataAdapter 填充的 DataTable,其中包含博客条目的 cmets 列表。由于某些原因,如果“匿名”字段设置为“1”,则用户名字段为空,应替换为指定的字符串。

我遇到的问题是,每当我尝试获取该字段的值时,我都会得到“真”或“假”。我的代码如下所示:

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();

【问题讨论】:

    标签: c# asp.net datatable datarow


    【解决方案1】:

    该字段可能是“位”字段类型,在 ADO.NET 中映射为布尔值

    只需检查真假:

    if ((bool)row["anonymous"])
       ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      相关资源
      最近更新 更多