By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,209 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,209 IT Pros & Developers. It's quick & easy.

Storing Data in Binary Field using ADODB

P: n/a
Hello everyone,

I am trying to store data in a binary field of an SQL-Server table using
ADODB.
So far, I have managed to store a record but not any data in the binary
field.
I only get exceptions on various part. I have tried both AppendChunk and
using a stream object.
Unfortunately all the examples I can find in the Net and on the Microsoft
site
are in VB and don't translate easily into C++.

Does anyone have an example where this is done in C++??
Here is my code if you want to check it it's pretty messy because I've been
trying all kinds of things:
---------------------------------------
#include <ctime>
#include <stdio.h>
#include <iostream>
#include <string.h>

#import "C:\Programme\Gemeinsame Dateien\System\ado\msado15.dll"
rename("EOF", "ADOEOF")

using namespace std;

#include <windows.h>

const AnzMesswerte = 50;
typedef struct {
unsigned int ZeitStempel;
short Uoc, Isc, Umpp, UdcIst, UausIst, Uiso, Iiso, Tin, Tk, Tmod[5];
short UGenIst[AnzMesswerte];
short IGenIst[AnzMesswerte];
unsigned int ZeitStempelEnde;
} SatzStruct;

typedef struct {
unsigned int sec;
unsigned short us, ms;
} ExakteZeit;

#define MaxRetries 10
#define SatzSize sizeof(Satz)
short retries = 0;
unsigned int temp_int;
unsigned int *int_ptr;
unsigned int sec, usec, usec2;
bool err = false;
HRESULT hr;
ExakteZeit Zeit;
SatzStruct Satz;

ADODB::_ConnectionPtr connection;
ADODB::_RecordsetPtr recordset;
ADODB::_StreamPtr mstream;
_variant_t varChunk;
void ConnectDB(void) {

CoInitialize(NULL);
try
{
hr = connection.CreateInstance(__uuidof(ADODB::Connecti on));
if (FAILED(hr)) throw _com_error(hr);
hr = recordset.CreateInstance(__uuidof(ADODB::Recordset ));
if (FAILED(hr)) throw _com_error(hr);
connection->CursorLocation = ADODB::adUseClient;
connection->Open(L"Provider=sqloledb;Data Source=localhost;"
L"Initial Catalog=Modulmessdaten;User Id=sa;Password=<Passwort>;", L"",
L"", ADODB::adConnectUnspecified);
recordset->Open(L"Messdaten", connection.GetInterfacePtr(),
ADODB::adOpenDynamic, ADODB::adLockOptimistic, ADODB::adCmdTable);
}
catch(...)
{
std::cerr << "Connect to database Exception";
}
}

void DatensatzSchreiben(short ModulNr) {

try {
recordset->AddNew();
recordset->Fields->GetItem(L"Tmod4")->PutValue(varChunk);
recordset->Fields->GetItem(L"UGenIst")->PutValue(mstream->Read(AnzMesswert
e));

recordset->Update();
}

catch(...)
{
std::cerr << "Update Exception";
}
}

int main() {
int i=1;
string l;
cout << "Connecting to database\n";
ConnectDB();

hr = mstream.CreateInstance(__uuidof(ADODB::Stream));
if (FAILED(hr)) throw _com_error(hr);
mstream->Type = ADODB::adTypeBinary;

UCHAR chData;
SAFEARRAY FAR *psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = AnzMesswerte;
psa = SafeArrayCreate(VT_UI2,1,rgsabound);
long index1 = 0;

://Copy the data only into Field.
for(long index=0;index<=(AnzMesswerte-1);index++)
{
//Take BYTE by BYTE and advance Memory Location
chData = Satz.UGenIst[index];
hr = SafeArrayPutElement(psa,&index1,&chData);
index1++;
}

varChunk.vt = VT_ARRAY|VT_UI2;
varChunk.parray = psa;

mstream->Write(varChunk);

DatensatzSchreiben(1);
recordset->Close();
connection->Close();
return 0;

}
Jul 23 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.