简体   繁体   English

如何将字符串转换为uniqueidentifier?

[英]How to convert string to uniqueidentifier?

I want to insert a string into a table as a uniqueidentifier type. 我想在表中插入一个字符串作为uniqueidentifier类型。 But when I insert it to the database, it throws an error. 但是,当我将其插入数据库时​​,它将引发错误。 How can i convert a string to a uniqueidentifier ? 如何将string转换为uniqueidentifier

You can try: 你可以试试:

new Guid("string to convert");

But the string will need to be in Guid format already. 但是字符串必须已经是Guid格式。

在.Net 4中,有一个Guid.TryParse(string, out Guid) ,它在成功时返回bool

This is a safe way to attempt parsing a string into a Guid . 这是尝试将string解析为Guid的安全方法。 In my example, input is a string variable from the user: 在我的示例中, input是来自用户的string变量:

var myGuid = new Guid();
if (Guid.TryParse(input, out myGuid)) {
    // Parsed OK
}

use one of these: 使用以下之一:

Guid.TryParse

or 要么

Guid.TryParseExact

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM