开发者

How to convert string to uniqueidentifier?

开发者 https://www.devze.com 2023-03-31 20:24 出处:网络
I want to insert a开发者_如何学Python string into a table as a uniqueidentifier type. But when I insert it to the database, it throws an error. How can i convert a string to a uniqueidentifier?You can

I want to insert a开发者_如何学Python string into a table as a uniqueidentifier type. But when I insert it to the database, it throws an error. How can i convert a string to a uniqueidentifier?


You can try:

new Guid("string to convert");

But the string will need to be in Guid format already.


In .Net 4, there's a Guid.TryParse(string, out Guid) which returns bool on success.


This is a safe way to attempt parsing a string into a Guid. In my example, input is a string variable from the user:

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


use one of these:

Guid.TryParse

or

Guid.TryParseExact
0

精彩评论

暂无评论...
验证码 换一张
取 消