c# - 在 asp.net 中将字符串转换为 SQL Server uniqueidentifier

标签 c# asp.net sql-server

asp.net 中是否有任何内置方法可以将字符串转换为 SQL Server 的 uniqueidentifier,就像 .ToString() 一样。

我收到一个错误:从字符串转换为 uniqueidentifier 时转换失败。

或者有没有人知道如何解决这个问题。谢谢。

最佳答案

Guid 有一个构造函数,它接受一个 string 作为参数。

Guid aGuid = new Guid(aString);

确保格式正确:

A string that contains a GUID in one of the following formats ("d" represents a hexadecimal digit whose case is ignored):

32 contiguous digits: dddddddddddddddddddddddddddddddd

-or-

Groups of 8, 4, 4, 4, and 12 digits with hyphens between the groups. The entire GUID can optionally be enclosed in matching braces or parentheses: dddddddd-dddd-dddd-dddd-dddddddddddd -or- {dddddddd-dddd-dddd-dddd-dddddddddddd} -or- (dddddddd-dddd-dddd-dddd-dddddddddddd)

-or-

Groups of 8, 4, and 4 digits, and a subset of eight groups of 2 digits, with each group prefixed by "0x" or "0X", and separated by commas. The entire GUID, as well as the subset, is enclosed in matching braces: {0xdddddddd, 0xdddd, 0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}

可以找到完整的文档here .

您可以将您的 RoleID guid 作为参数传入:

Guid roleID = new Guid(roleIDAsString);
sqlCommand.Parameters.Add("@RoleID", SqlDbType.UniqueIdentifier).Value = roleID;

关于c# - 在 asp.net 中将字符串转换为 SQL Server uniqueidentifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8091668/

相关文章:

c# - 使用 NPPExec 脚本从 Notepad++ 中使用 csc.exe 编译 C#

c# - 在 windows phone 8.1 中,Windows phone 项目和 silverlight 项目有什么区别?

c# - Windows Phone 8 滚动文本框的内容

javascript - 如何在不离开 ASP.NET C# 焦点的情况下触发 TextChange 事件?

mysql - MySQL 和 MSSQL 之间的分布式事务

c# - 使用带有 C# 的 Selenium WebDriver 迭代链接

asp.net - 跨浏览器CSS

ASP.NET 在全局范围内为 Gridview 和 DetailsView 重用相同的样式

c# - SQL 服务器通知

.net - 如何从 Windows docker 容器中的 .NET 应用程序连接到主机上运行的 SQL Server