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/