Assuming that your sql statement cannot return DBNull.Value, then yes you can:
Guid myResult = (Guid) cmd.ExecuteScalar();
EDIT: Now that we know you need to handle nulls.... :-)
I can think of two ways to handle nulls - use a nullable Guid and set it to null, or use a regular Guid and have it set to Guid.Empty if your SQL statement returns null.
Consider some form of helper function or extension method which checks for 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 datatype other than UniqueIdentifier.