I have a string containing a regular SQL-Statement with Guids. The statement should be converted so I can run it against a local SQLite database which cannot handle Guids as described by .NET-Framework. Therefore I have to convert them to their binary representation, which means that the Guid (in SQL) '00000000-0000-0000-0000-000000000000' will become X'00000000000000000000000000000000' for SQLite or 'FE334797-0A46-468D-91F2-0005F1EC67EC' will become X'974733fe460a8d4691f20005f1ec67ec'.
The method to convert a single Guid is as follows:
private static string GetBinaryGuid(Guid guidToConvert)
{
var guidBytes = guidToConvert.ToByteArray();
var guidBinary = new StringBuilder();
foreach (var guidByte in guidBytes)
{
guidBinary.AppendFormat(@"{0}", guidByte.ToString("x2"));
}
return guidBinary.ToString();
}
The method to find the real Guid(s) in the query-string is:
resultString = Regex.Replace(subjectString, @"\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b", "'$0'", RegexOptions.IgnoreCase);
My question is how to replace the "real" Guid(s) in the string with their respective binary equivalent?
Edit: Just to clarify. I want to fetch all Guid in the string, pass the found Guids to the method mentioned above and replace it within the string. The result should be the SQL-Query with the binary Guid(s) if found in the string.
Edit2:
SELECT * FROM table WHERE Id = 'FE334797-0A46-468D-91F2-0005F1EC67EC'
should become
SELECT * FROM table WHERE Id = X'974733fe460a8d4691f20005f1ec67ec'
Edit3:
@aelor got me the right direction. For my specific case the solution can be found here.