Given a SQL Server database that uses uniqueidentifiers as primary key fields for the table I want to query and a GUID struct (included via rpc.h) that has the value of one of the entries in the table, how do I create a ADODB::Parameter that stores that value?
I understand that the parameters take their values as _variant_t. How do I get from the GUID to a _variant_t? A cast produces an error and the _variant_t type does not take a GUID in the constructor.
I am creating the command and parameter objects in the following way:
HRESULT hr;
_CommandPtr selectCmd;
hr = selectCmd.CreateInstance(__uuidof(ADODB::Command));
if (FAILED(hr))
throw _com_error开发者_JAVA技巧(hr);
selectCmd->CommandText = L"SELECT .... WHERE id=@id";
selectCmd->Parameters->Append(selectCmd->CreateParameter(L"@id",DataTypeEnum::adGUID, ParameterDirectionEnum::adParamInput, 16, /* what to put here? */));
Pass a string, formatted with curly braces. If you have the guid value stored in a variable of type System.Guid then you can use the "B" format specifier. Like this:
var arg = guid.ToString("B");
See this web page for reference.
Oops, that's C# code, you can generate the string in C++ by using StringFromIID().
精彩评论