如何在sql server中对guid进行base64编码

发布于 2025-01-03 15:49:01 字数 322 浏览 4 评论 0原文

如何在 sql server 2005 中使用 uniqueidentifier 数据类型重新创建以下 c# 代码。

string encoded = Convert.ToBase64String(guid.ToByteArray());

我尝试了在互联网上遇到的几个示例,例如 http://www.vbforums.com/showthread.php?t=554886 但它们都返回比.net 功能。

How can I re-create the following c# code with a uniqueidentifier datatype in sql server 2005.

string encoded = Convert.ToBase64String(guid.ToByteArray());

I have tried several examples I came across on the internet such as http://www.vbforums.com/showthread.php?t=554886 but they all return strings much longer than the .net function.

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

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

发布评论

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

评论(3

星軌x 2025-01-10 15:49:01

标识符,则链接中的示例有效

Guid guid = Guid.NewGuid();
string encoded = Convert.ToBase64String(guid.ToByteArray());
Console.WriteLine(guid);
Console.WriteLine(encoded);

唯一

32705fe3-cedd-4bfd-a881-9dd11e543f9e
419wMt3O/UuogZ3RHlQ/ng==

如果您传入And

declare @guid uniqueidentifier = '32705fe3-cedd-4bfd-a881-9dd11e543f9e'
select dbo.f_BinaryToBase64(@guid)

for 的

419wMt3O/UuogZ3RHlQ/ng==

The sample in your link works if you pass in a uniqueidentifier

Guid guid = Guid.NewGuid();
string encoded = Convert.ToBase64String(guid.ToByteArray());
Console.WriteLine(guid);
Console.WriteLine(encoded);

for

32705fe3-cedd-4bfd-a881-9dd11e543f9e
419wMt3O/UuogZ3RHlQ/ng==

And

declare @guid uniqueidentifier = '32705fe3-cedd-4bfd-a881-9dd11e543f9e'
select dbo.f_BinaryToBase64(@guid)

for

419wMt3O/UuogZ3RHlQ/ng==
黎夕旧梦 2025-01-10 15:49:01

我的 guid 位于名为“guid”的 nvarchar(MAX) 列中,看起来像这样

 {4A449FE1-8989-4D69-935C-9E918244DEED}

采取几个步骤,即显示我的工作。我在表 guidreal、guidbinary、guidbase64 中添加了三列。我意识到这可以被压缩:-)。

1.

update [Database].[dbo].[LotofRecords]
set guidreal =  Cast(Replace(Replace([guid],'{',''),'}','') AS UNIQUEIDENTIFIER)

2.

update [Database].[dbo].[LotofRecords]
set guid_binary =  cast(guidreal as VARBINARY(MAX))

3.

update [Database].[dbo].[LotofRecords]
set guidBase64 =   cast(N'' as XML).value(
              'xs:base64Binary(xs:hexBinary(sql:column("guid_binary")))'
              ,'nvarchar(25)'
              )

这让我

4Z9ESomJaU2TXJ6RgkTe7Q==

使用以下命令解码回 varbinary

select cast(N'' as XML).value(
              'xs:base64Binary(sql:column("guidbase64"))'
              ,'varbinary(max)'
              )
              ,guid_binary
from
 [Database].[dbo].[LotofRecords]

my guid's was in a nvarchar(MAX) column named 'guid' and looked like this

 {4A449FE1-8989-4D69-935C-9E918244DEED}

Taking a few steps i.e. showing my working. I added three columns to my table guidreal, guidbinary, guidbase64. I realise this can be compressed :-).

1.

update [Database].[dbo].[LotofRecords]
set guidreal =  Cast(Replace(Replace([guid],'{',''),'}','') AS UNIQUEIDENTIFIER)

2.

update [Database].[dbo].[LotofRecords]
set guid_binary =  cast(guidreal as VARBINARY(MAX))

3.

update [Database].[dbo].[LotofRecords]
set guidBase64 =   cast(N'' as XML).value(
              'xs:base64Binary(xs:hexBinary(sql:column("guid_binary")))'
              ,'nvarchar(25)'
              )

which gave me

4Z9ESomJaU2TXJ6RgkTe7Q==

decode back to varbinary using

select cast(N'' as XML).value(
              'xs:base64Binary(sql:column("guidbase64"))'
              ,'varbinary(max)'
              )
              ,guid_binary
from
 [Database].[dbo].[LotofRecords]
━╋う一瞬間旳綻放 2025-01-10 15:49:01

我可能是错的,但我认为这是不可能的。
C# 端的算法与您在 sql 部分中找到的任何组合都不同。


为了解决这个问题,我建议将所有事情都集中在一侧。即:从 C# 端填充数据库或在 sql 部分执行所有操作。

I might be wrong but I think it's not possible.
The algorithm in C# side is different from whatever combination you may find in sql part.


To solve this, what I recommend is to do all in one side. i.e.: populate the db from the c# side or do all in the sql part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文