开发者

How to convert string to uniqueidentifier?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜