I assigned some value to object data type like,
object objData =dc.GetDirectoryEntry().Properties[\"objectGUID\"].Value;
this object retun the
The System.DirectoryServices.DirectoryEntry
class has the property Guid
for this purpose - no need to access the objectGUID attribute through Properties
.
Though the manual casting suggested above works, there's a way to do this automatically.
SQLiteConnectionStringBuilder
to configure a Connection that understands your Guids as Guids:var builder = new SQLiteConnectionStringBuilder("Data Source=./mydatabase.db") { BinaryGUID = true };
var connStr = builder.ToString();
return new SQLiteConnection(connStr);
Here's the official SQLite provider: https://www.nuget.org/packages/System.Data.SQLite.Core/
public class GuidTypeHandler : SqlMapper.TypeHandler<Guid>
{
public override Guid Parse(object value)
{
var valueAsBytes = (byte[])value;
return new Guid(valueAsBytes);
}
public override void SetValue(System.Data.IDbDataParameter parameter, Guid value)
{
var guidAsBytes = value.ToByteArray();
parameter.Value = guidAsBytes;
}
}
// And the registration in Startup.cs or equivalent:
SqlMapper.AddTypeHandler<Guid>(new GuidTypeHandler());
Source: Dapper Issue #718 - GitHub
How about using the Guid constructor which takes a byte array?
Guid guid = new Guid(binaryData);
(You can then use Guid.ToString()
to get it in text form if you need to.)
byte[] binaryData = objData as byte[];
string strHex = BitConverter.ToString(binaryData);
Guid id = new Guid(strHex.Replace("-", ""))
The long form would be (enter link description here):
public static string ConvertGuidToOctectString(string objectGuid)
{
System.Guid guid = new Guid(objectGuid);
byte[] byteGuid = guid.ToByteArray();
string queryGuid = "";
foreach (byte b in byteGuid)
{
queryGuid += @"\" + b.ToString("x2");
}
return queryGuid;
}