如何用ado将二进制图片数据存入mysql数据库

如何用ado将二进制图片数据存入mysql数据库,第1张

用ADO方式连的MYSQL, 已经用AppendChunk图片以二进制插入数据库中,下面是读取的不分,写入相反的方式

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10013023.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存