将 StringBuilder 转换为 bool 时出错

问题描述 投票:0回答:0

我必须将数据库中列的布尔值传输到客户端应用程序(Angular)。

在我的服务器端:

    public class Data
    {
      public bool connDB_valid { get; set; }
    }  

 StringBuilder sbvalid = new StringBuilder();
 while (reader.Read())
    {
      if (sbvalid.Length > 0)
        sbvalid.Append(", "); // delimiter

      sbvalid.Append(reader.GetBoolean(reader.GetOrdinal("DB_Valid")));     

      data.connDB_valid = Convert.ToBoolean(sbvalid.ToString());
      System.Diagnostics.Debug.WriteLine("**GETconnDB_Valid : " + sbvalid);//here I get correctly the column from DB (False, True, True)
    }
    return Ok(data);

使用上面的代码我得到以下错误:

抛出异常:mscorlib.dll 中的“System.FormatException” ***错误连接数据库:System.FormatException:字符串未被识别为有效的布尔值。在 System.Boolean.Parse(字符串 值)在 System.Convert.ToBoolean(字符串值)在 ServerSide.Controllers.GetConnectionDBController.GetConnDB() 在 项目路径

This topic or this one 没有帮助我解决错误。我如何将所有

sbvalid
的值传递给
data.connDB_valid

c# type-conversion stringbuilder
© www.soinside.com 2019 - 2024. All rights reserved.