void DatabaseFile::GetImage()
{
_RecordsetPtr pRs = NULL
pRs.CreateInstance(__uuidof(Recordset))
_variant_t varChunk
_variant_t varBLOB
try
{
CString sql = "select image from visdata where DetectTime = '2008-06-19 11:23:44'"
//Open a Recordset
HRESULT hr = pRs->Open(_variant_t("select * from visdata"),_variant_t((IDispatch *) m_pConnection,true),adOpenKeyset,adLockOptimistic,adCmdText)
//read data
long lDataLength = pRs->Fields->GetItem("image")->ActualSize//数据长度可以正确获取
varBLOB = pRs->GetFields()->GetItem("image")->GetChunk(lDataLength)
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
BYTE *pBuf = NULL
pBuf = (BYTE*)GlobalAlloc(GMEM_FIXED,lDataLength)//分配空间
hr = SafeArrayAccessData(varBLOB.parray,(void **)&pBuf)
if (FAILED(hr))
return
//Build a File
char tmpPath[_MAX_PATH+1]
GetCurrentDirectory( MAX_PATH,tmpPath)
CString strFileName = "\\temp2.bmp"//输出图片的文件名
strFileName = tmpPath+strFileName
CFile outFile(strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary )
/*要判断文件是否创建成功*/
LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)(pBuf))
outFile.WriteHuge(buffer,lDataLength)
GlobalUnlock((HGLOBAL)pBuf)
outFile.Close()
SafeArrayUnaccessData (varBLOB.parray)
}
pRs->Close()
}
dim cn As New ADODB.Connection'声明dim rs As New ADODB.Recordset
'打开数据库
Private Sub OpenConn()
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.CursorLocation = adUseClient
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0Data Source=数据库文件路径Jet OLEDB:DataBase Password=密码Persist Security Info=False"
End Sub
'关闭数据库
Public Sub CloseConn()
If rs.State <>sckClosed Then rs.Close
Set rs = Nothing
If cn.State <>sckClosed Then cn.Close
Set cn = Nothing
End Sub
Private Sub Command1_Click()
Call OpenConn'打开数据库
rs.Open "select 字段名 from 表名", cn, 3, 3
rs.AddNew
rs.Fields("字段名") = Text1.Text
rs.Update'写入
Call CloseConn'关闭数据库
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)