Ado 实现C++对象的存取.docx
《Ado 实现C++对象的存取.docx》由会员分享,可在线阅读,更多相关《Ado 实现C++对象的存取.docx(13页珍藏版)》请在冰豆网上搜索。
Ado实现C++对象的存取
Ado实现C++对象的存取
作者:
江上飞鸟
下载源代码
其实我最讨厌写东西了,最近忙死了,呵呵,前一段时间在做一个图形程序时,需要把C++对象保存到数据库里,刚开始真让我头疼啊,琢磨了一个下午,终于给做出来了,废话不说了,还是把自己的一些体会与各位同任一起分享!
!
!
!
一、新建一个继承于CObject的子类CLine;
头文件:
Line.h
classCLine:
publicCObject?
{
private:
LOGPENm_logPen;//画笔
COLORREFm_crBackColor;
CArraym_PointArray;//标记类对应框
public:
intGetSize();
CPointGetPoint(intpos);
voidDrawLine(CDC*pDC,CPointpt1,CPointpt2,CRectrc);
voidDrawBackGround(CDC*pDC,CRectrect);
voidDrawPoint(CDC*pDC,CRectrect);
voidSetWidth(intiWidth);
COLORREFGetColor();
voidSetColor(COLORREFcolor);
COLORREFGetBkColor();
voidSetBkColor(COLORREFcolor);
voidAddPoint(CPointpoint);
voidClear();
CLine();
virtual~CLine();
virtualvoidSerialize(CArchive&ar);
CLine&operator=(CLine&src);
DECLARE_SERIAL(CLine)?
?
};
实现文件:
Line.cpp
//////////////////////////////////////////////////////////////////////
//Line.cpp:
implementationoftheCLineclass.
//
//////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include"TestAdo.h"
#include"Line.h"
#ifdef_DEBUG
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#definenewDEBUG_NEW
#endif
IMPLEMENT_SERIAL(CLine,CObject,1)
CLine:
:
CLine()
{
Clear();
}
CLine:
:
~CLine()
{
}
//重写=
CLine&CLine:
:
operator=(CLine&src)
{
if(this!
=&src)
{
m_logPen=src.m_logPen;
m_crBackColor=src.m_crBackColor;
}
return*this;?
}
//串行化操作
voidCLine:
:
Serialize(CArchive&ar)
{
if(ar.IsStoring())
{
ar<ar.Write(&m_logPen,sizeof(LOGPEN));
}
else
{
DWORDdw;
ar>>dw;m_crBackColor=COLORREF(dw);
ar.Read(&m_logPen,sizeof(LOGPEN));
}
m_PointArray.Serialize(ar);
}
voidCLine:
:
Clear()
{
m_crBackColor=RGB(255,255,255);
m_logPen.lopnStyle=PS_SOLID;
m_logPen.lopnWidth.x=1;
m_logPen.lopnWidth.y=1;
m_logPen.lopnColor=RGB(0,0,0);
m_PointArray.RemoveAll();
}
voidCLine:
:
AddPoint(CPointpoint)
{
m_PointArray.Add(point);
}
voidCLine:
:
SetColor(COLORREFcolor)
{
m_logPen.lopnColor=color;
}
COLORREFCLine:
:
GetColor()
{
returnm_logPen.lopnColor;
}
voidCLine:
:
SetBkColor(COLORREFcolor)
{
m_crBackColor=color;
}
COLORREFCLine:
:
GetBkColor()
{
returnm_crBackColor;
}
voidCLine:
:
SetWidth(intiWidth)
{
m_logPen.lopnWidth.x=iWidth;
m_logPen.lopnWidth.y=iWidth;
}
//绘线条
voidCLine:
:
DrawPoint(CDC*pDC,CRectrect)
{
intlen=m_PointArray.GetSize();
if(len<=0)return;
CPenpen;
pen.CreatePenIndirect(&m_logPen);
CPen*pOldPen=pDC->SelectObject(&pen);
CPointpt=m_PointArray.GetAt(0);
pDC->MoveTo(pt);
for(inti=1;i{
pt=m_PointArray.GetAt(i);
pDC->LineTo(pt);
}
pDC->SelectObject(pOldPen);
pOldPen=NULL;
pen.DeleteObject();
}
voidCLine:
:
DrawBackGround(CDC*pDC,CRectrect)
{
CBrushbrushCtl;
brushCtl.CreateSolidBrush(GetBkColor());
pDC->Rectangle(rect);
pDC->FillRect(rect,&brushCtl);
brushCtl.DeleteObject();
}
voidCLine:
:
DrawLine(CDC*pDC,CPointpt1,CPointpt2,CRectrc)
{
CPenpen;
pen.CreatePenIndirect(&m_logPen);
CPen*pOldPen=pDC->SelectObject(&pen);
pDC->MoveTo(pt1);
pDC->LineTo(pt2);
pDC->SelectObject(pOldPen);
pOldPen=NULL;
pen.DeleteObject();
}
CPointCLine:
:
GetPoint(intpos)
{
if(pos>=0&&pos{
returnm_PointArray.GetAt(pos);
}
returnCPoint(0,0);
}
intCLine:
:
GetSize()
{
returnm_PointArray.GetSize();
}
二、用Ado接口打开数据库
BOOLCTestAdoDlg:
:
OpenDb(CStringfilename)
{
HRESULThr=S_OK;
hr=m_pCon.CreateInstance("ADODB.Connection");
if(hr!
=S_OK)
{
returnFALSE;
}
try
{
_bstr_tsCon;
sCon=_bstr_t(filename);//路径名
sCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+sCon;
hr=m_pCon->Open(sCon,"","",adModeUnknown);
if(hr!
=S_OK)
{
returnFALSE;
}
///////////////////////
hr=m_pSet.CreateInstance("ADODB.Recordset");
if(hr!
=S_OK)
{
returnFALSE;
}
m_pSet->CursorLocation=adUseClient;
hr=m_pSet->Open("SELECT*FROMobject_table",_variant_t((IDispatch*)m_pCon,TRUE),
adOpenStatic,adLockOptimistic,adCmdText);
if(hr!
=S_OK)
{
returnFALSE;
}
returnTRUE;
///////////////////////
}
catch(_com_error&e)
{
CStringerrorMessage;
errorMessage.Format("连接数据库失败!
错误信息:
%s",e.ErrorMessage());
returnFALSE;
}
returnFALSE;
}
(注意:
在StdAfx.h中要加入:
#import"C:
\ProgramFiles\CommonFiles\SYSTEM\ADO\msado15.dll"no_namespacerename("EOF","adoEOF")
来引入ado库,还有在BOOLCTestAdoApp:
:
InitInstance()加入AfxOleInit();///初始化COM库)
三、CLine对象的保存
voidCTestAdoDlg:
:
OnButtonSave()
{
//m_List
if(!
m_bState)return;
UpdateData();
try
{
m_pSet->AddNew();
m_pSet->PutCollect("name",_variant_t(m_sName));
//保存图形对象
CMemFilememFile;
CArchivear(&memFile,CArchive:
:
store);
m_Line.Serialize(ar);
ar.Close();
DWORDdwSize=memFile.GetLength();
LPBYTElpInfo=memFile.Detach();
VARIANTvarBLOB;
SAFEARRAY*psa;
SAFEARRAYBOUNDrgsabound[1];
rgsabound[0].lLbound=0;
rgsabound[0].cElements=dwSize;
psa=SafeArrayCreate(VT_UI1,1,rgsabound);
for(longi=0;i<(long)dwSize;i++)
{
SafeArrayPutElement(psa,&i,lpInfo++);
}
varBLOB.vt=VT_ARRAY|VT_UI1;
varBLOB.parray=psa;
m_pSet->GetFields()->GetItem("object")->AppendChunk(varBLOB);
m_pSet->Update();
m_List.AddString(m_sName);
}
catch(_com_error&e)
{
CStringstr=(char*)e.Description();
MessageBox(str+"\r保存数据库出问题!
","提示",MB_OK|MB_ICONWARNING);
return;
}
}
四、CLine对象的读取
voidCTestAdoDlg:
:
OnSelchangeListData()
{
intiPos=m_List.GetCurSel();
if(iPos<0)return;
m_pSet->MoveFirst();
inti=0;
while(i{
m_pSet->MoveNext();
i++;
}
longlDataSize=m_pSet->GetFields()->GetItem(_variant_t("object"))->ActualSize;
if(lDataSize<=0)return;
_variant_tvarBLOB;
VariantInit(&varBLOB);
varBLOB=m_pSet->GetFields()->GetItem(_variant_t("object"))->GetChunk(lDataSize);
if(varBLOB.vt==(VT_ARRAY|VT_UI1))
{
BYTE*pBuf=newBYTE[lDataSize+1];
if(pBuf)
{
SafeArrayAccessData(varBLOB.parray,(void**)&pBuf);
SafeArrayUnaccessData(varBLOB.parray);
CMemFilememfile;
memfile.Attach(pBuf,lDataSize);
memfile.SeekToBegin();
CArchivear(&memfile,CArchive:
:
load);
m_Line.Serialize(ar);
ar.Close();
memfile.Detach();
CRectrc=GetRect(IDC_STATIC_RECT);
InvalidateRect(rc);
}
}
VariantClear(&varBLOB);
}
五、结束语
以上充分利用了串行化来实现c++对象保存到数据库,对以上方法稍做扩展对图象的保存到数据库,甚至多个图象文件保存到数据库和文件。