问题描述
我有一个SQL Server 2012数据库.每个表都有一组审核字段.一个是名为RowVer的列,其数据类型为timestamp
(与rowversion
相同).
I have a SQL Server 2012 database. Each table has a set of audit fields. One is a column named RowVer with a datatype of timestamp
(same as rowversion
).
我很难将SqlDataReader
中的值读取到具有名为User.RowVersion
且数据类型为Byte[]
的属性的C#对象中.
I'm having difficulty reading the value from a SqlDataReader
into a C# object with a property named User.RowVersion
with a datatype of Byte[]
.
while (rdr.Read())
块的代码段
...
user.DateCreated = rdr.GetDateTime(14);
user.CreatedByUserId = rdr.GetInt32(15);
if (!rdr.IsDBNull(16)) { user.DateLastUpdated = rdr.GetDateTime(16); }
if (!rdr.IsDBNull(17)) { user.LastUpdatedByUserId = rdr.GetInt32(17); }
user.RowVersion = rdr.???;
...
RowVersion值仅在更新数据库时用作并发比较.
The RowVersion value is only used as a comparison for concurrency when updating the database.
这当然不是EF实现.
推荐答案
如果您使用的是.NET 4.5或更高版本,则可以这样做
If you are using .NET 4.5 or newer you can do
user.RowVersion = rdr.GetFieldValue<byte[]>(18);
如果您使用的是4.0或更早的版本,则需要执行投射.
If you are on 4.0 or older you will need to perform a cast.
user.RowVersion = rdr.GetValue(18) as byte[];
这篇关于如何从SQLDataReader到C#变量读取Rowversion或时间戳记SQL Server数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!