[英]Why does my DataTable always returns “true/false” but never a string?
I'm trying to use a DataTable, filled by a MySqlDataAdapter, which contains a list of comments for a blog entry. 我正在尝试使用由MySqlDataAdapter填充的DataTable,其中包含博客条目的注释列表。 For some reasons, if the field "anonymous" is set to "1", the username field is empty and should be replaced with a specified string.
由于某些原因,如果字段“ anonymous”设置为“ 1”,则用户名字段为空,应使用指定的字符串替换。
The problem I have is that whenever I try to get the value of the field, I get either "true" or "false". 我的问题是,每当我尝试获取该字段的值时,都会得到“ true”或“ false”。 My code looks like this:
我的代码如下所示:
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();
The field is probably a "bit" field type, which maps to Boolean in ADO.NET 该字段可能是“位”字段类型,它映射到ADO.NET中的Boolean。
Simply check for true or false: 只需检查是非即可:
if ((bool)row["anonymous"])
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.