SQL Server unique-identifier equivalent in C#

System.Guid

No conversions needed.


System.Guid

When reading nullable Uniqueidentifier columns from your database, be sure to check if the value is null before attempting to assign to a Guid instance, as Guids are not nullable. For example:

... /// using recordset rs


// generates exception if rs["my_guid"] is null
Guid g = (Guid)rs["my_guid"];

// returns Guid.Empty {0000000-.....} if db value is null
Guid g = (Guid)(rs["my_guid"] ?? Guid.Empty); 

etc.


If you are getting the value from a SQLDataReader, make sure to check it against DBNull before you try to use it. Sometimes the value can be interpreted as a string as well, so you need to type New Guid(rs["my_guid"]) to make sure you have a guid to use in your code.