简体   繁体   English

将SQL Server ROWVERSION放入System.Data.Linq.Binary属性时,出现System.InvalidCastException

[英]System.InvalidCastException when getting SQL Server ROWVERSION into System.Data.Linq.Binary property

I am using IDataReader to select all from SQL Server DB which contain a Version column with Data Type ROWVERSION Here is My code: 我正在使用IDataReader从SQL Server DB中选择所有数据库,其中包含具有数据类型ROWVERSION的Version列,这是我的代码:

ItemDetail.cs ItemDetail.cs

private Binary _version;
public Binary Version
{
    get { return _version; }
    set { _version = value; }
}

public ItemDetails(int ItemID, string AddedBy, DateTime AddingDate, 
  string LastUpdateBy, DateTime LastUpdateDate,Binary Version)
{
    this.ItemID = ItemID;
    this.AddedBy = AddedBy;
    this.AddingDate = AddingDate;
    this.LastUpdateBy = LastUpdateBy;
    this.LastUpdateDate = LastUpdateDate;
    this.Version = Version;
}

ItemProvider.cs ItemProvider.cs

protected virtual ItemDetails GetItemFromReader(IDataReader Reader)
{
    return new ItemDetails(
       (int)Reader["ItemID"],
       Reader["AddedBy"].ToString(),
       (DateTime)Reader["AddingDate"],
       Reader["LastUpdateBy"].ToString(),
       (DateTime)Reader["LastUpdateDate"],
       (byte[])Reader["Version"]);
}
protected virtual List<ItemDetails> GetItemCollectionFromReader(IDataReader Reader)
{
    List<ItemDetails> Items = new List<ItemDetails>();
    while (Reader.Read())
    {
        Items.Add(GetItemFromReader(Reader));
    }
    return Items;
}

I got 我有

System.InvalidCastException in GetItemFromReader(IDataReader Reader) at (byte[])Reader["Version"]) GetItemFromReader(IDataReader Reader)中位于(byte [])Reader [“ Version”])的System.InvalidCastException

How can I fix this? 我怎样才能解决这个问题? Thank You 谢谢

After looking through many posts here and there I found the solution which is: 在各处浏览许多帖子后,我找到了解决方案:

ItemProvider.cs replace: ItemProvider.cs替换为:

(byte[])Reader["Version"])

with: 有:

new Binary((byte[])Reader["Version"])

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

相关问题 服务器上的System.InvalidCastException - System.InvalidCastException On Server SQL 服务器 (ORM) select 来自数据库的数据 - System.InvalidCastException - SQL Server (ORM) select data from database - System.InvalidCastException 从SQL Server 2012 Express检索数据时出现“ System.InvalidCastException”错误 - “System.InvalidCastException” error when retrieving data from SQL Server 2012 Express 将System.Drawing.Image转换为System.Data.Linq.Binary - Converting System.Drawing.Image to System.Data.Linq.Binary LINQ,聚合操作不支持“System.Data.Linq.Binary”类型 - The type 'System.Data.Linq.Binary' is not supported in aggregation operations - LINQ 无法在LINQ中将类型&#39;string&#39;隐式转换为&#39;System.Data.Linq.Binary - Cannot implicitly convert type 'string' to 'System.Data.Linq.Binary in LINQ to SQL 如何将&#39;byte []&#39;类型转换为&#39;System.Data.Linq.Binary&#39; - How to convert type 'byte[]' to 'System.Data.Linq.Binary' .NET Core中的等效System.Data.Linq.Binary - Equivalent System.Data.Linq.Binary in .net core Linq To数据集错误System.InvalidCastException:指定的强制转换无效 - Linq To Data set error System.InvalidCastException: Specified cast is not valid 类型“ System.Data.Linq.Binary”在未引用的程序集中定义 - Type 'System.Data.Linq.Binary' is defined in an assembly that is not referenced
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM