简体   繁体   English

从SQL Server 2012 Express检索数据时出现“ System.InvalidCastException”错误

[英]“System.InvalidCastException” error when retrieving data from SQL Server 2012 Express

I am trying to test a method that retrieves some info from a SQL Server 2012 Express instance and "adds" it into a list. 我正在尝试测试一种从SQL Server 2012 Express实例检索一些信息并将其“添加”到列表中的方法。 The problem is that when I try to run the test I get the error: 问题是当我尝试运行测试时出现错误:

"System.InvalidCastException" “ System.InvalidCastException”

Here is the code: 这是代码:

List<Reference> lst = new List<Reference>();
//DB Connections is a class instance that takes care of the
//connection with the db.
DBConnections db = new DBConnections(DbConfig.conStr);
db.UpdateCmd("SELECT * FROM freelancers_references");

SqlDataReader dr = db.ExecuteReader();

if (dr.HasRows) {
    lst = dr.Cast<IDataRecord>().Select(r =>
        new Reference {
            Id = (int)dr["id"],
            OwnerId = (int)dr["owner_id"],
            Name = (string)dr["name"],
            Active = (bool)dr["active"]
        }).ToList<Reference>();
}

dr.Close();
return lst;   

EDIT: @Dan provided a working answer. 编辑:@丹提供了一个有效的答案。 Though using a Convert.ToInt/ToString() etc method did the trick as well. 尽管使用Convert.ToInt / ToString()等方法也能达到目的。

Check if columns allows null. 检查列是否允许为空。 If so, make the type nullable Ie 如果是这样,则将类型设置为可为空

OwnerId = (int?)dr["owner_id"],
DBConnections db = new DBConnections(DbConfig.conStr);
db.UpdateCmd("SELECT * FROM freelancers_references");
SqlDataReader dr = db.ExecuteReader();
if (dr.HasRows) {
    lst = dr.OfType<IDataRecord>().Select(r =>
        new Reference {
            Id = (int)dr["id"],
            OwnerId = (int)dr["owner_id"],
            Name = (string)dr["name"],
            Active = (bool)dr["active"]
        }).ToList();
}
dr.Close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL 服务器 (ORM) select 来自数据库的数据 - System.InvalidCastException - SQL Server (ORM) select data from database - System.InvalidCastException 将SQL Server ROWVERSION放入System.Data.Linq.Binary属性时,出现System.InvalidCastException - System.InvalidCastException when getting SQL Server ROWVERSION into System.Data.Linq.Binary property 服务器上的System.InvalidCastException - System.InvalidCastException On Server 从Session状态检索字典时,我得到System.InvalidCastException - I get System.InvalidCastException, when retrieving a dictionary from Session state System.InvalidCastException:将数据发布到sql表时,指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid when posting data to sql table 错误:GetCharacters System.InvalidCastException:指定的强制类型转换无效的服务器 - Error: GetCharacters System.InvalidCastException: Specified cast is not valid server SQL查询的转换结果-System.InvalidCastException - Casting Result from SQL Query - System.InvalidCastException 如何解决错误“System.InvalidCastException - 列包含 NULL 数据” - How to solve error "System.InvalidCastException - column contains NULL data" Linq To数据集错误System.InvalidCastException:指定的强制转换无效 - Linq To Data set error System.InvalidCastException: Specified cast is not valid C#数组中的System.InvalidCastException错误 - System.InvalidCastException error in c# array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM