如何使用C#从sql server读取时间戳类型的数据?

发布于 2024-12-14 08:36:20 字数 390 浏览 0 评论 0原文

我在 .NET 中得到的结果如下:

var lastRowVersion = SqlHelper.ExecuteScalar(connStr, CommandType.Text, "select
top 1 rowversion from dbo.sdb_x_orginfo order by rowversion desc");

结果是一个字节数组 [0]= 0,[1]=0,[2]=0,[3]=0,[4]=0,[ 5]=0,[6]=30,[7]=138,但 SQL Server 中的结果是0x0000000000001E8A

如何在 .NET 中获取值 "0x0000000000001E8A"

I get the result in .NET like this:

var lastRowVersion = SqlHelper.ExecuteScalar(connStr, CommandType.Text, "select
top 1 rowversion from dbo.sdb_x_orginfo order by rowversion desc");

The result is a byte array [0]= 0,[1]=0,[2]=0,[3]=0,[4]=0,[5]=0,[6]=30,[7]=138, but the result in SQL Server is 0x0000000000001E8A.

How can I get the value "0x0000000000001E8A" in .NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

九八野马 2024-12-21 08:36:20

我发现从 sql server 返回的 byte[] 具有错误的 Endian-ness,因此转换为 long (Int64) 无法正常工作。我通过在将数组传递到 BitConverter 之前调用 Reverse 解决了这个问题:

byte[] byteArray = {0, 0, 0, 0, 0, 0, 0, 8};

var value = BitConverter.ToUInt64(byteArray.Reverse().ToArray(), 0);

另外,我认为最好转换为 UInt64。

I found that the byte[] returned from sql server had the wrong Endian-ness and hence the conversion to long (Int64) did not work correctly. I solved the issue by calling Reverse on the array before passing it into BitConverter:

byte[] byteArray = {0, 0, 0, 0, 0, 0, 0, 8};

var value = BitConverter.ToUInt64(byteArray.Reverse().ToArray(), 0);

Also, I thought it better to convert to UInt64.

甜点 2024-12-21 08:36:20

如果您只想将 byte[] 转换为 System.Int64 (又名 long),则使用 BitConverter.ToInt64

SqlBinary binary = /* ... */;
long value = BitConverter.ToInt64(binary.Value, 0); // 0 is the start index in the byte array

要将其显示为十六进制字符串,您可以可以使用X 格式说明符,例如:

Console.WriteLine("Value is 0x{0:X}.", value);

If you just want to convert byte[] to a System.Int64 (aka long) then use BitConverter.ToInt64:

SqlBinary binary = /* ... */;
long value = BitConverter.ToInt64(binary.Value, 0); // 0 is the start index in the byte array

To display it as a hex string, you can use the X format specifier, e.g.:

Console.WriteLine("Value is 0x{0:X}.", value);
凉宸 2024-12-21 08:36:20

这是一句俏皮话:

var byteArray = new byte[] { 0, 0, 0, 0, 0, 0, 30, 138 };

var rowVersion = "0x" + string.Concat(Array.ConvertAll(byteArray, x => x.ToString("X2")));

结果是:

"0x0000000000001E8A"

请注意,有 其他性能更好的选项

Here's an one-liner:

var byteArray = new byte[] { 0, 0, 0, 0, 0, 0, 30, 138 };

var rowVersion = "0x" + string.Concat(Array.ConvertAll(byteArray, x => x.ToString("X2")));

The result is:

"0x0000000000001E8A"

Just be aware that there are other options that perform better.

遥远的她 2024-12-21 08:36:20

您可以使用以下查询,该查询将返回 bigint 而不是返回十六进制。

从 dbo.sdb_x_orginfo order by rowversion desc 中选择前 1 个类型转换(rowversion as bigint)rowversion

You can use following query which will return bigint instead of returning hex.

select top 1 cast(rowversion as bigint) rowversion from dbo.sdb_x_orginfo order by rowversion desc

回心转意 2024-12-21 08:36:20

与“Ashish Singh”相同,我在 DB 上转换为 bigint 后返回到 c#,并在代码端使用 Int64。
在 c#/VB 中转换为 long/Int64/UInt64 给出了错误的结果。(即使在由于小端问题而反转数组之后。)

请注意 DB 端 RowVersion 时间戳具有 MIN_ACTIVE_ROWVERSION() ,它指示最后提交的时间戳

Same as "Ashish Singh" I returned to c# after converting to bigint on DB and used Int64 on the code side.
Converting to long/Int64/UInt64 in c#/VB gave wrong results.(even after reversing the array because of the little endian issue.)

Pay attention on the DB side that RowVersion timestamp has MIN_ACTIVE_ROWVERSION() which indicates the last commited timestamp

层林尽染 2024-12-21 08:36:20

对我来说效果很好

  var versionString = new StringBuilder();
  versionString.Append("0x");
  for (var i = 0; i < binary.Count(); i++)
  {
      versionString.Append(string.Format("{0:X}", binary[i]));
  }
  versionString.ToString(); // result

Works fine for me

  var versionString = new StringBuilder();
  versionString.Append("0x");
  for (var i = 0; i < binary.Count(); i++)
  {
      versionString.Append(string.Format("{0:X}", binary[i]));
  }
  versionString.ToString(); // result
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文