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
精彩评论