Click here to Skip to main content
16,016,894 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionException handling or return value Pin
Raj Prathap21-May-07 2:32
Raj Prathap21-May-07 2:32 
AnswerRe: Exception handling or return value Pin
CPallini21-May-07 3:10
mveCPallini21-May-07 3:10 
GeneralRe: Exception handling or return value Pin
Raj Prathap21-May-07 18:22
Raj Prathap21-May-07 18:22 
GeneralRe: Exception handling or return value Pin
Nelek21-May-07 19:53
protectorNelek21-May-07 19:53 
GeneralRe: Exception handling or return value Pin
CPallini21-May-07 20:46
mveCPallini21-May-07 20:46 
GeneralRe: Exception handling or return value Pin
Raj Prathap21-May-07 23:17
Raj Prathap21-May-07 23:17 
QuestionGroupbox flickering Pin
baerten21-May-07 2:31
baerten21-May-07 2:31 
QuestionHow to insert a binary file into SQL database, via ADO Pin
lilnelse21-May-07 2:15
lilnelse21-May-07 2:15 
I want to insert a binary file, say "tt.bin", into a field (image type) of SQL database, I wrote a program as follows

C++, VS2003, ADO, input file: tt.bin,database: moldb,
Create table moltbl (id INT, mol IMAGE);
But I didn't get nothing in the output file: new.bin

where am I wrong, please? thanks very much!

// BLOB.cpp

#import "c:/Program Files/Common Files/System/ADO/msado15.dll" \
no_namespace rename("EOF", "EndOfFile")

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
::CoInitialize(NULL);

_RecordsetPtr m_pRecordset("ADODB.Recordset");
_ConnectionPtr m_pConnection("ADODB.Connection");
_bstr_t bstrSQL("select * from moltbl");

ifstream moldata;
moldata.open("tt.bin", ios::in|ios::binary);
ofstream newdata;
newdata.open("new.bin", ios::out|ios::binary);

char buffer[10240];
char *pBuf = buffer;
long nFileLen = 0;

if (!moldata.fail() && !moldata.eof())
moldata.read(buffer,10240);
nFileLen = moldata.gcount();

VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
int index = 0;
_bstr_t strConnect = "Provider=SQLOLEDB; Server=ELSE-HP;"
"Database=moldb; uid=sa; pwd=sa";
m_pConnection->Open(strConnect, "", "", adModeUnknown);
if(m_pConnection == NULL)
cerr<<"Load data ERROR!\n";
HRESULT hr = m_pRecordset->Open("SELECT * FROM moltbl",
_variant_t((IDispatch *)m_pConnection,true),
adOpenDynamic,
adLockPessimistic,adCmdText);
m_pRecordset->AddNew();

m_pRecordset->PutCollect("id",_variant_t(index++));

if(pBuf)
{
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nFileLen;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < (long)nFileLen; i++)

SafeArrayPutElement (psa, &i, pBuf++);
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa;
m_pRecordset->GetFields()->GetItem("mol")->AppendChunk(varBLOB);
}

m_pRecordset->Update();

long lDataSize =_pRecordset->GetFields()->GetItem("mol")->ActualSize;

ifstream newfile;
newfile.open("new.bin", ios::out| ios::binary);
if(lDataSize > 0)
{
_variant_t varBLOB;
varBLOB = m_pRecordset->GetFields()->GetItem("mol")->GetChunk(lDataSize);

if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{


SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);

newdata.write(pBuf, lDataSize);
SafeArrayUnaccessData (varBLOB.parray);
}
}

moldata.close();
newdata.close();
return 0;
}

Hi, there. Need your help!

QuestionRe: How to insert a binary file into SQL database, via ADO Pin
David Crow21-May-07 3:03
David Crow21-May-07 3:03 
AnswerRe: How to insert a binary file into SQL database, via ADO Pin
lilnelse21-May-07 16:56
lilnelse21-May-07 16:56 
AnswerRe: How to insert a binary file into SQL database, via ADO Pin
lilnelse21-May-07 17:05
lilnelse21-May-07 17:05 
QuestionRead Excel file Pin
Syamlal S Nair21-May-07 2:14
Syamlal S Nair21-May-07 2:14 
AnswerRe: Read Excel file Pin
David Crow21-May-07 3:08
David Crow21-May-07 3:08 
AnswerRe: Read Excel file Pin
Hamid_RT21-May-07 6:54
Hamid_RT21-May-07 6:54 
AnswerRe: Read Excel file Pin
Sameerkumar Namdeo21-May-07 21:07
Sameerkumar Namdeo21-May-07 21:07 
QuestionVC6 to VC8: how to set two libs in the VC8 to be linked with the project? Pin
Joan M21-May-07 2:02
professionalJoan M21-May-07 2:02 
AnswerRe: VC6 to VC8: how to set two libs in the VC8 to be linked with the project? Pin
JudyL_MD21-May-07 2:10
JudyL_MD21-May-07 2:10 
AnswerRe: VC6 to VC8: how to set two libs in the VC8 to be linked with the project? Pin
Cedric Moonen21-May-07 2:11
Cedric Moonen21-May-07 2:11 
QuestionHow to retain the values Pin
jannathali21-May-07 1:56
jannathali21-May-07 1:56 
AnswerRe: How to retain the values Pin
Cedric Moonen21-May-07 1:58
Cedric Moonen21-May-07 1:58 
AnswerRe: How to retain the values Pin
CPallini21-May-07 2:19
mveCPallini21-May-07 2:19 
QuestionRe: How to retain the values Pin
Roger Stoltz21-May-07 2:27
Roger Stoltz21-May-07 2:27 
AnswerRe: How to retain the values Pin
Hamid_RT21-May-07 6:45
Hamid_RT21-May-07 6:45 
QuestionCListCtrl Selection of Item Problem Pin
Aamol M21-May-07 1:35
Aamol M21-May-07 1:35 
AnswerRe: CListCtrl Selection of Item Problem Pin
GuyM21-May-07 1:46
GuyM21-May-07 1:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.