Assuming your sql statement cannot return DBNull.Value, then yes you can:
Guid myResult = (Guid) cmd.ExecuteScalar();
EDIT: Now that we know you need to process zeros .... :-)
I can think of two ways to handle zeros: use a nullable Guid and set it to null or use a regular Guid and set it to Guid.Empty if your SQL statement returns null.
Consider some form of helper function or extension method that checks DBNull.Value.
static Guid? GetGuidFromDb(object dbValue) { if (dbValue == null || DBNull.Value.Equals(dbValue)) { return null; } else { return (Guid) dbValue; } }
or
static Guid GetGuidFromDb(object dbValue) { if (dbValue == null || DBNull.Value.Equals(dbValue)) { return Guid.Empty; } else { return (Guid) dbValue; }
Then call
Guid? myResult = GetGuidFromDb(cmd.ExecuteScalar());
Note. This will choke if your SQL command returns a data type other than UniqueIdentifier.
source share