📄 关于用vc,vb进行图像数据(二进制大对象)存储数据库的一点心得 .htm
字号:
<HTML>
<HEAD>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
<meta HTTP-EQUIV='Expires' CONTENT=0>
<link rel='stylesheet' href='../../../../news.css'>
<style type='text/css'>
.fst{padding:0px 15px;width:770px;background:#eeeecc;border-left:1px solid #000000;border-right:1px solid #000000}
.fstdiv3 img{border:0px;border-right:8px solid #eeeecc;border-top:6px solid #eeeecc}
</style>
<title>
关于用VC,VB进行图像数据(二进制大对象)存储数据库的一点心得
</title>
</HEAD>
<BODY aLink=#990000 bgColor=#ffffff bottomMargin=0 leftMargin=0 rightMargin=0 topMargin=0 marginwidth=0 marginheight=0>
<center>
<TABLE align=center bgColor=#cccc99 border=0 cellPadding=2 cellSpacing=0 width=770>
<TBODY>
<TR>
<TH align=left >
关于用VC,VB进行图像数据(二进制大对象)存储数据库的一点心得
</TH>
</TD></TR>
</TBODY></TABLE>
<TABLE align=center bgColor=#eeeecc border=1 cellPadding=1 cellSpacing=0 width=770>
<TBODY>
<TR><TD colSpan=5 >
<a href='http://www.csdn.net'>中国软件开发网络</a>
-->
<a href='http://www.csdn.net/develop'>开发图书馆</a>
-->
<a href='http://www.csdn.net\develop\list_article.asp?lib=1&bigclassid=
1
'>
Visual C++
</a>
-->
<a href='http://www.csdn.net\develop\list_article.asp?lib=1&bigclassid=
1
&smallclassid=
107
'>
数据库操作
</a>
-->
<a href='
http://www.csdn.net/develop/library/vc/datebase/6863.shtm
'>
关于用VC,VB进行图像数据(二进制大对象)存储数据库的一点心得
</a>
</TD></tr>
<TR>
<TD align=left width=300>
<B>关键字:</B><BR>
ADO,BLOB,图像数据
</TD>
<TD align=middle width=120>
<B>贴文时间</B><br>
2001-5-5 16:05:09
</TD>
<TD align=middle width=80>
<B>文章类型: </B><BR>
原作
</TD>
<TD align=middle width=100>
<B>给贴子投票 </B>
<BR><a href='http://www.csdn.net/develop/addscore.asp?id=
6863
'>投票</a>
</TD></TR>
<TR>
<TD >
bluestar
原作
</TD>
<TD colSpan=3 vAlign=top>
<B>出处: </B><A href='
'>
</A></TD></TR>
<TR><TD colSpan=5 bgcolor=#cccc99> </TD></TR>
</TD></TR></TBODY></TABLE>
<div align=center><div class=fst align=left><div class=fstdiv3 id=print2>
<br><br><P> 网上经常有人问如何把图像存入数据库中,原先我也是不得要领。经过多方指点和自己在开发过程中的摸索,终于解决这一问题。</P>
<P> 下面给出用VC,VB如何操作图像文件存取数据库的原码,帮助一些还没有掌握方法的朋友,也请这方面的高手多多指教。(均用ADO连接数据库)</P>
<P><FONT face=System>1. VC把一个文件存入数据库</FONT></P>
<P><FONT face=System> CFile imagefile;<BR> if(0 == imagefile.Open("d:\\user\\bmp.bmp",CFile::modeRead))<BR> return;<BR> _RecordsetPtr pRs = NULL; <BR> _ConnectionPtr pConnection = NULL;<BR> _variant_t varChunk;<BR> HRESULT hr;<BR> BYTE* pbuf;<BR> long nLength = imagefile.GetLength();<BR> pbuf = new BYTE[nLength+2];<BR> if(pbuf == NULL)<BR> return; //allocate memory error;<BR> imagefile.Read(pbuf,nLength); //read the file into memory</FONT></P>
<P><FONT face=System> BYTE *pBufEx;<BR> pBufEx = pbuf;<BR> //build a SAFFERRAY<BR> SAFEARRAY* psa;<BR> SAFEARRAYBOUND rgsabound[1];<BR> rgsabound[0].lLbound = 0;<BR> rgsabound[0].cElements = nLength;<BR> psa = SafeArrayCreate(VT_UI1, 1, rgsabound);</FONT></P>
<P><FONT face=System> for (long i = 0; i < nLength; i++)<BR> SafeArrayPutElement (psa, &i, pBufEx++);<BR> VARIANT varBLOB;<BR> varBLOB.vt = VT_ARRAY | VT_UI1;<BR> varBLOB.parray = psa;</FONT></P>
<P><FONT face=System> _bstr_t strCnn("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER"); <BR> try<BR> {<BR> //Open a connection<BR> pConnection.CreateInstance(__uuidof(Connection));<BR> hr = pConnection->Open(strCnn,"","",NULL); //Connect a DataBase<BR> pRs.CreateInstance(__uuidof(Recordset));<BR> pRs->Open("CustomInfo",_variant_t((IDispatch *) pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable); //Open a Table<BR> <BR>// pRs->AddNew(); <BR> pRs->Fields->GetItem("Image")->AppendChunk(varBLOB); <BR> pRs->Update();<BR> pRs->Close();<BR> pConnection->Close();<BR> }<BR> catch(_com_error &e)<BR> {<BR> // Notify the user of errors if any.<BR> _bstr_t bstrSource(e.Source());<BR> _bstr_t bstrDescription(e.Description());<BR> CString sError;<BR> sError.Format("Source : %s \n Description : %s\n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);<BR> AfxMessageBox(sError); <BR> }</FONT></P>
<P><FONT face=System>2. VC把数据库中IMAGE字段取出存为文件</FONT></P>
<P><FONT face=System> _RecordsetPtr pRs = NULL;<BR> _ConnectionPtr pConnection = NULL;<BR> _variant_t varChunk;<BR> HRESULT hr;<BR> VARIANT varBLOB;<BR> _bstr_t strCnn("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER"); <BR> try<BR> {<BR> //Open a connection<BR> pConnection.CreateInstance(__uuidof(Connection));<BR> hr = pConnection->Open(strCnn,"","",NULL); <BR> pRs.CreateInstance(__uuidof(Recordset));<BR> pRs->Open("CustomInfo",_variant_t((IDispatch *) pConnection,true),adOpenKeyset,adLockOptimistic,adCmdTable);<BR> //read data <BR> long lDataLength = pRs->Fields->GetItem("Image")->ActualSize;<BR> varBLOB = pRs->GetFields()->GetItem("Image")->GetChunk(lDataLength);<BR> if(varBLOB.vt == (VT_ARRAY | VT_UI1)) <BR> {<BR> BYTE *pBuf = NULL; <BR> pBuf = (BYTE*)GlobalAlloc(GMEM_FIXED,lDataLength);<BR> SafeArrayAccessData(varBLOB.parray,(void **)pBuf); <BR> //Build a File in Windows Temp Directory<BR> char tmpPath[_MAX_PATH+1];<BR> GetTempPath(_MAX_PATH,tmpPath);<BR> CString strFileName = "temp.bmp";<BR> strFileName = tmpPath+strFileName;<BR> <BR> CFile outFile(strFileName,CFile::modeCreate|CFile::modeWrite);<BR> LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)pBuf);<BR> outFile.WriteHuge(buffer,lDataLength);<BR> GlobalUnlock((HGLOBAL)pBuf);<BR> outFile.Close(); <BR> SafeArrayUnaccessData (varBLOB.parray);<BR> }</FONT></P>
<P><FONT face=System> pRs->Close();<BR> pConnection->Close();<BR> }<BR> catch(_com_error &e)<BR> {<BR> // Notify the user of errors if any.<BR> _bstr_t bstrSource(e.Source());<BR> _bstr_t bstrDescription(e.Description());<BR> CString sError;<BR> sError.Format("Source : %s \n Description : %s\n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);<BR> AfxMessageBox(sError); <BR> } </FONT></P>
<P><FONT face=System>3. VB把文件存入数据库IMAGE字段</FONT></P>
<P><FONT face=System>Sub savepic(FileName As String, IndexNumber As Long)<BR> Dim DcnNWind As New ADODB.Connection<BR> Dim rs As ADODB.Recordset<BR> Set rs = New ADODB.Recordset<BR> DcnNWind.CursorLocation = adUseClient<BR> DcnNWind.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER"<BR> rs.CursorType = adOpenKeyset<BR> rs.LockType = adLockOptimistic<BR> rs.Open "CustomInfo", DcnNWind, , adCmdTable<BR> rs.Move (IndexNumber)<BR> Call FileToBlob(rs.Fields("Image"), FileName, FileLen(FileName))<BR> rs.UpdateBatch adAffectCurrent<BR>End Sub</FONT></P>
<P><FONT face=System>Private Sub FileToBlob(fld As ADODB.Field, FileName As String, Optional ChunkSize As Long )<BR> Dim fnum As Integer, bytesLeft As Long, bytes As Long<BR> Dim tmp() As Byte<BR> If (fld.Attributes And adFldLong) = 0 Then<BR> Err.Raise 1001, , "Field doesn't support the GetChunk method."<BR> End If<BR> fnum = FreeFile<BR> Open FileName For Binary As fnum<BR> bytesLeft = LOF(fnum)<BR> Do While bytesLeft<BR> bytes = bytesLeft<BR> If bytes > ChunkSize Then bytes = ChunkSize<BR> ReDim tmp(1 To bytes) As Byte<BR> Get #1, , tmp<BR> fld.AppendChunk tmp<BR> bytesLeft = bytesLeft - bytes<BR> Loop<BR> Close #fnum<BR>End Sub</FONT></P>
<P><FONT face=System>4. VB把文件从IMAGE字段中读到文件中。</FONT></P>
<P><FONT face=System>Sub loadpic(IndexNumber As Long)<BR> Dim DcnNWind As New ADODB.Connection<BR> Dim rs As ADODB.Recordset<BR> Set rs = New ADODB.Recordset<BR> DcnNWind.CursorLocation = adUseClient<BR> DcnNWind.Open "Provider=SQLOLEDB.1;Integrated Security=SSI;Persist Security Info=False;Initial Catalog=CUSTOM;Data Source=SERVER"<BR> rs.CursorType = adOpenKeyset<BR> rs.LockType = adLockOptimistic<BR> rs.Open "CustomInfo", DcnNWind, , adCmdTable<BR> rs.Move (IndexNumber)<BR> Call BlobToFile(rs.Fields("Image"), "c:\windows\temp\tmp.bmp", rs.Fields("Image").ActualSize)<BR>End Sub</FONT></P>
<P><FONT face=System>Private Sub BlobToFile(fld As ADODB.Field, FileName As String, Optional ChunkSize As Long )<BR> Dim fnum As Integer, bytesLeft As Long, bytes As Long<BR> Dim tmp() As Byte<BR> If (fld.Attributes And adFldLong) = 0 Then<BR> Err.Raise 1001, , "Field doesn't support the GetChunk method."<BR> End If<BR> If Dir$(FileName) <> "" Then Kill FileName<BR> fnum = FreeFile<BR> Open FileName For Binary As fnum<BR> bytesLeft = fld.ActualSize<BR> Do While bytesLeft<BR> bytes = bytesLeft<BR> If bytes > ChunkSize Then bytes = ChunkSize<BR> tmp = fld.GetChunk(bytes)<BR> Put #fnum, , tmp<BR> bytesLeft = bytesLeft - bytes<BR> Loop<BR> Close #fnum<BR>End Sub</FONT></P><br><br>
</DIV></div></div>
<script src='../../../get_readnum.asp?id=
6863
'></script>
</center></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -