SQL Server的varbinary数据类型可以存储哪些数据?

发布于 2024-09-10 12:24:45 字数 112 浏览 1 评论 0原文

我有一个表,其中 userpassword 字段具有 varbinary 数据类型,所以我很困惑应该以哪种形式将数据保存到 userpassword 字段中,因为当我保存 varchar 数据时,它给了我错误。

I have a table in which the userpassword field have varbinary datatype, So I'm confused that in which form should I save the data into userpassword field because when I save varchar data it gave me error.

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

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

发布评论

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

评论(4

伴梦长久 2024-09-17 12:24:45

varbinary 列可以存储任何内容。要在其中存储字符串,您必须将其转换为 varbinary

declare @t table (id int identity, pwd varbinary(50))
insert into @t (pwd) values (cast('secret' as varbinary(50)))

但对于密码,varbinary 列通常存储某种哈希值。例如,使用 的 SHA1 哈希HashBytes函数

insert into @t (pwd) values (HashBytes('sha1', 'secret'));

存储单向哈希而不是真实密码更安全。您可以检查密码是否匹配:

select * from @t where pwd = HashBytes('sha1', 'secret')

但是您无法通过查看表来检索密码。因此只有最终用户知道他的密码,甚至 DBA 也无法检索它。

A varbinary column can store anything. To store a string in it, you'd have to cast it to varbinary:

declare @t table (id int identity, pwd varbinary(50))
insert into @t (pwd) values (cast('secret' as varbinary(50)))

But for a password, a varbinary column usually stores a hash of some kind. For example, a SHA1 hash using the HashBytes function:

insert into @t (pwd) values (HashBytes('sha1', 'secret'));

Storing a one-way hash instead of the real password is more secure. You can check if the password matches:

select * from @t where pwd = HashBytes('sha1', 'secret')

But there is no way you can retrieve the password by looking at the table. So only the end user knows his password, and not even the DBA can retrieve it.

我的黑色迷你裙 2024-09-17 12:24:45

您需要显式转换 VARCHAR。

SELECT CAST(N'Test' as VARBINARY)

SQL Server 错误消息显示。

从数据类型 varchar 到 varbinary 的隐式转换不是
允许。

You will need to explicitly cast the VARCHAR.

SELECT CAST(N'Test' as VARBINARY)

SQL Server error message says.

Implicit conversion from data type varchar to varbinary is not
allowed.

忘东忘西忘不掉你 2024-09-17 12:24:45

SQL Server 需要从 varchar 到 varbinary,根据 MSDN 中 CAST 和 CONVERT 上的大表

该表将有一个 varbinary 列来存储哈希值,根据 sys.sql_logins

SQL Server requires an explicit conversion from varchar to varbinary, as per the big table on CAST and CONVERT in MSDN

The table will have a varbinary column to store hashed values as per sys.sql_logins

送你一个梦 2024-09-17 12:24:45
SELECT CAST(N'Test' as VARBINARY)

------你必须给它一个尺寸---------->

SELECT CAST(N'Test' as VARBINARY(30))
SELECT CAST(N'Test' as VARBINARY)

------you have to give it a size ---------->

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