做SQL2005与vs的连接,想利用ADO接口实现向库中存储image二进制大文件,查到的帖子都是说创建Connection对象连接数据库,并用到以下语句添加代码,
BOOL CADOTest1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
我是用MFC应用程序单文档模板,解决方案名为ADODemo,在程序ADODemo.cpp里就没找到CADOTest1Dlg类,
这个类中间部分是不是根据工程名称改变呀,比如在我的工程要改成CADODemoDlg类,可是也找不到!!
是不是不在这个文件里?找不到这个类的话,我自己的代码添加在哪?
根本还不熟悉vc,可是有赶着要做用到访问数据库的东西,找到一篇说得还算详细的帖子
http://www.vckbase.com/document/viewdoc/?id=215 代码倒是快弄明白了,就是不知道自己编的代码改填在哪。
BOOL CADOTest1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
我是用MFC应用程序单文档模板,解决方案名为ADODemo,在程序ADODemo.cpp里就没找到CADOTest1Dlg类,
这个类中间部分是不是根据工程名称改变呀,比如在我的工程要改成CADODemoDlg类,可是也找不到!!
是不是不在这个文件里?找不到这个类的话,我自己的代码添加在哪?
根本还不熟悉vc,可是有赶着要做用到访问数据库的东西,找到一篇说得还算详细的帖子
http://www.vckbase.com/document/viewdoc/?id=215 代码倒是快弄明白了,就是不知道自己编的代码改填在哪。
BYTE *pBufEx;
pBufEx = pbuf;
//build a SAFFERRAY
SAFEARRAY* psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nLength;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < nLength; i++)
SafeArrayPutElement (psa, &i, pBufEx++);//将pBuf指向的二进制数据保存到SAFEARRAY对象psa中
VARIANT varBLOB;
varBLOB.vt = VT_ARRAY | VT_UI1;//将varBLOB的类型设置为BYTE类型的数组
varBLOB.parray = psa;_RecordsetPtr pRs = NULL;
_ConnectionPtr pConnection = NULL;
_variant_t varChunk;
HRESULT hr;
try
{
pConnection.CreateInstance(__uuidof(Connection)); //初始化Connection指针,创建实例
_bstr_t strCnn("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=xn;Data Source="); //定义数据库连接字符串
hr = pConnection->Open(strCnn,"","",NULL); //使用Connection对象的Open方法来进行连接数据库,连接已定义字符串的数据库 pRs.CreateInstance(__uuidof(Recordset)); //初始化Recordset指针
pRs->Open("SELECT * FROM image",_variant_t((IDispatch *) pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable); //Open a Table
//直接用Recordset对象进行查询取得记录集,查询数据库xn中的image表
while(!pRs->adoEOF)
{
//pRs->AddNew();
pRs->Fields->GetItem("ImageData")->AppendChunk(varBLOB); //向ImageData字段读入二进制大对象BLOB
//pRs->GetFields()->GetItem("ImageData")->AppendChunk(varBLOB);
pRs->Update();
pRs->Close();
pConnection->Close();}
}
catch(_com_error &e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
CString sError;
sError.Format("Source : %s Description : %s ",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
AfxMessageBox(sError);
}
以上是我根据网上的代码,稍改下凑出来的代码,请前辈们指点
自己先顶下~~~
有本书 很大篇幅介绍了 ADO mfc编程 蛮详细的 我没用到就没怎么看 你要看 我可以告诉你书名
{
CDialog::OnInitDialog();
//读取硬盘文件到内存
CFile imagefile;
if(0 == imagefile.Open(L"d:\\user\\bmp.bmp",CFile::modeRead))
return; 有错误
BYTE* pbuf;
long nLength = imagefile.GetLength();
pbuf = new BYTE[nLength+2];
if(pbuf == NULL)
return; //allocate memory error;
imagefile.Read(pbuf,nLength); //read the file into memory//创建SAFEARRAY对象,以便在网络环境中使用数组型参数,在使用中,还需要将其再包装到VARIANT类型的变量
BYTE *pBufEx;
pBufEx = pbuf;
//build a SAFFERRAY
SAFEARRAY* psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nLength;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < nLength; i++)
SafeArrayPutElement (psa, &i, pBufEx++);//将pBuf指向的二进制数据保存到SAFEARRAY对象psa中
VARIANT varBLOB;
varBLOB.vt = VT_ARRAY | VT_UI1;//将varBLOB的类型设置为BYTE类型的数组
varBLOB.parray = psa;_RecordsetPtr pRs = NULL;
_ConnectionPtr pConnection = NULL;
_variant_t varChunk;
HRESULT hr;
try
{
pConnection.CreateInstance(__uuidof(Connection)); //初始化Connection指针,创建实例
_bstr_t strCnn("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=xn;Data Source=202.114.13.223"); //定义数据库连接字符串
hr = pConnection->Open(strCnn,"","",NULL); //使用Connection对象的Open方法来进行连接数据库,连接已定义字符串的数据库 pRs.CreateInstance(__uuidof(Recordset)); //初始化Recordset指针
pRs->Open("SELECT * FROM image",_variant_t((IDispatch *) pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable); //Open a Table
//直接用Recordset对象进行查询取得记录集,查询数据库xn中的image表
while(!pRs->adoEOF)
{
//pRs->AddNew();
pRs->Fields->GetItem("ImageData")->AppendChunk(varBLOB); //向ImageData字段读入二进制大对象BLOB
//pRs->GetFields()->GetItem("ImageData")->AppendChunk(varBLOB);
pRs->Update();
pRs->Close();
pConnection->Close();}
}
catch(_com_error &e)
{
// Notify the user of errors if any.
CString sError;
/*_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
sError.Format("Source : %s Description : %s ",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);*/
sError.Format(_T("Source : %s \n Description : %s "),(LPCSTR)e.Source(),
(LPCSTR)e.Description());
AfxMessageBox(sError);
}
1>------ 已启动生成: 项目: inputimage, 配置: Debug Pocket PC 2003 (ARMV4) ------
1>正在编译...
1>inputimageDlg.cpp
1>.\inputimageDlg.cpp(41) : error C2561: 'CinputimageDlg::OnInitDialog' : function must return a value
1> g:\vc++\vs\inputimage\inputimage\inputimageDlg.h(25) : see declaration of 'CinputimageDlg::OnInitDialog'
1>.\inputimageDlg.cpp(43) : warning C4244: 'initializing' : conversion from 'ULONGLONG' to 'long', possible loss of data
1>.\inputimageDlg.cpp(46) : error C2561: 'CinputimageDlg::OnInitDialog' : function must return a value
1> g:\vc++\vs\inputimage\inputimage\inputimageDlg.h(25) : see declaration of 'CinputimageDlg::OnInitDialog'