本文介绍了从数据库中获取数据时将字符串转换为bool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的数据库表是 创建 table tasktodo(Id int identity ( 1 , 1 )不 null ,完成位,文本 nvarchar (max),日期日期) i写了一个代码来从数据库中获取数据c# public 列表< TaskToDoList> GetTaskToDo() { var Obj = DBHelper.GetDBObject(); reader = Obj.ExecuteReader(CommandType.StoredProcedure, GetTaskToDoList); var tasktodo = new 列表< TaskToDoList>(); while (reader.Read()) { tasktodo.Add( new TaskToDoList { Id = GetInteger( Id),完成= Convert.ToBoolean( 完成 ), Text = GetString( Text), Date = reader [ Date] == DBNull.Value?DateTime.MinValue:Convert.ToDateTime(reader [ 日期])}); } return tasktodo; } 我的模特 命名空间 SampleMVCApplication.Models { public class TaskToDoList { public int Id { get ; set ; } public bool 完成{获得; set ; } public string 文本{获得; set ; } [DataType(DataType.Date)] public DateTime Date { get ; set ; } } } 显示错误字符串未被识别为有效布尔值 如何解决这个问题解决方案 我得到了答案 public 列表< tasktodolist> GetTaskToDo() { var Obj = DBHelper.GetDBObject(); reader = Obj.ExecuteReader(CommandType.StoredProcedure, GetTaskToDoList); var tasktodo = new 列表< tasktodolist>(); while (reader.Read()) { tasktodo.Add( new TaskToDoList { Id = GetInteger( Id), Done = Convert.ToBoolean(reader [ 完成]), Text = GetString( Texts), Date = reader [ 日期] == DBNull.Value?DateTime.MinValue:Convert.ToDateTime( reader [ 日期])}); } return tasktodo; } < / tasktodolist > < / tasktodolist > my database table iscreate table tasktodo(Id int identity(1,1) not null, Done bit,Texts nvarchar(max),Dates date)i wrote a code to get the data from database in c#public List<TaskToDoList> GetTaskToDo() { var Obj = DBHelper.GetDBObject(); reader = Obj.ExecuteReader(CommandType.StoredProcedure, "GetTaskToDoList"); var tasktodo = new List<TaskToDoList>(); while(reader.Read()) { tasktodo.Add( new TaskToDoList { Id =GetInteger("Id"), Done=Convert.ToBoolean("Done"), Text=GetString("Text"), Date =reader["Date"] ==DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["Date"]) }); } return tasktodo; }my modelnamespace SampleMVCApplication.Models{ public class TaskToDoList { public int Id { get; set; } public bool Done { get; set; } public string Text { get; set; } [DataType(DataType.Date)] public DateTime Date { get; set; } }}it is showing an error "String was not recognized as a valid Boolean"how to solve this 解决方案 I got the answerpublic List<tasktodolist> GetTaskToDo() { var Obj = DBHelper.GetDBObject(); reader = Obj.ExecuteReader(CommandType.StoredProcedure, "GetTaskToDoList"); var tasktodo = new List<tasktodolist>(); while(reader.Read()) { tasktodo.Add( new TaskToDoList { Id =GetInteger("Id"), Done=Convert.ToBoolean( reader["Done"]), Text=GetString("Texts"), Date =reader["Dates"] ==DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["Dates"]) }); } return tasktodo; }</tasktodolist></tasktodolist> 这篇关于从数据库中获取数据时将字符串转换为bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-15 12:17