RE: Storing array of doubles as a blob using ADO
- From: Marek <dnv21@xxxxxxxxxxxxxxxx>
- Date: Wed, 11 Feb 2009 03:11:01 -0800
Hi Jialang
I should have said that the exception:
"Binary stream '0' does not contain a valid BinaryHeader. Possible causes
are invalid stream or object version change between serialization and
deserialization."
on the line:
blobData = (double[])binaryFormatter.Deserialize(memoryStream);
brought this to my attention.
Also the binary formatter needs to be initialised at the top of the C# code:
BinaryFormatter binaryFormatter = new BinaryFormatter();
Best regards
Marek
""Jialiang Ge [MSFT]"" wrote:
Hello Marek.
I have tested your code snippet on my side and can reproduce the error.
Although I'm currently not 100% sure about cause of the error, I figure out
a solution after a couple of hours' tests and researches:
The solution is to change all the appearances of double and VT_R8 to byte
and VT_I1. Then the code works well in my test. I doubt that
command->CreateParameter always expects a *byte* array (BLOB), instead of a
double array, when the parameter type is specified as adLongVarBinary. If
you indeed need to initialize the array as double, we can convert it to be
an byte array when we create the safearray:
===================================
double blobData[40000];
// init the blog data
int size = sizeof(blobData);
// the safe array's var type is specified as byte
SAFEARRAY *pSA = SafeArrayCreateVector(VT_I1, 0, size);
byte *pData = (byte*)blobData;
hr = SafeArrayAccessData(pSA, (void **)&pData);
memcpy(pData, blobData,size);
SafeArrayUnaccessData(pSA);
_variant_t var;
// Assign the Safe array to a variant.
var.vt = VT_ARRAY|VT_I1;
var.parray = pSA;
ADODB::_ParameterPtr spParam;
spParam = spCommand->CreateParameter(_bstr_t("Blob"),
ADODB::adLongVarBinary,
ADODB::adParamInput, size , var);
spCommand->Parameters->Append(spParam);
====================================
By the way, I notice several other problems in your code while I was
testing it. They are not directly related to the "Application uses a value
of the wrong type for the current operation" error, but they may deserve
your notice:
1.
TCHAR* sql = TEXT("INSERT INTO [BlobTable] ([ObjectKey], [Blob]) VALUES
('CFD6DC2F-52E2-4406-A4E3-B663F457EAF5', ?)");
command->CommandText = sql;
command->CommandText expects a bstr, but you are passing a LPCTSTR to it.
This may cause potential problems:
http://blogs.msdn.com/ericlippert/archive/2003/09/12/52976.aspx
2.
SAFEARRAY *pSA = SafeArrayCreateVector(VT_R8, 0, sizeof(blobData));
If I understand it rightly, you are creating a double array sized
RECORD_COUNT. Because blobData is a double array, sizeof(blobData) returns
the byte count that equals RECORD_COUNT * 8. Thus, it results in a double
array sized RECORD_COUNT * 8. This may not be what you wanted.
3.
// Copy the data to the SAFEARRAY.
double *pData;
hr = SafeArrayAccessData(pSA, (void **)&pData);
It appears that you forgot to point pData to the double array?
Please try my solution and let me know whether it works for you. If you
have any additional questions or concerns, feel free to tell me.
P.S. some additional readings:
http://support.microsoft.com/kb/190450
The problem in the KB aritcle looks the same as this one, but they are
actually different. That article results from the way that *VB* calculates
the size of an array.
Have a nice day!
Regards,
Jialiang Ge (jialge@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
- References:
- Storing array of doubles as a blob using ADO
- From: Marek
- RE: Storing array of doubles as a blob using ADO
- From: "Jialiang Ge [MSFT]"
- Storing array of doubles as a blob using ADO
- Prev by Date: RE: Storing array of doubles as a blob using ADO
- Next by Date: RE: Storing array of doubles as a blob using ADO
- Previous by thread: RE: Storing array of doubles as a blob using ADO
- Next by thread: Displaying data in a web form
- Index(es):
Relevant Pages
|