用BLOB类型。 GetChunk(long lSize)读取数据,AppendChunk写入。 下面是MSDN中的例子,你可以在网上找到VC的: HOWTO: Retrieving Bitmap from Access and Displaying In Web Page Last reviewed: December 11, 1997 Article ID: Q175261 The information in this article applies to: Microsoft Visual InterDev, version 1.0 Microsoft Active Server Pages, version 1.0b Microsoft Visual Basic Professional and Enterprise Editions for Windows, version 5.0 SUMMARY This article shows by example how to extract the bitmap photos in the Microsoft Access 97 Northwind.mdb database, and view them from a Web browser using Active Server Pages (ASP). In order to accomplish this task, an ActiveX DLL must be created that strips the Access and OLE headers from the field. This article shows how to create this ActiveX DLL, and how to implement it. MORE INFORMATION WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This article demonstrates how to use Visual Basic to retrieve a bitmap stored in an OLE Object field. Because the definition of OLE object storage is not documented, the following code searches the object's OLE header for characters consistent with the start of the graphic. This method may not work in all circumstances. Be aware that Internet Explorer 3.0 is unable to display true color bitmaps. For this reason, the bitmaps stored in the Access database should be no higher than 256 colors. Step-by-Step Example to Extract the PhotosCreate a new project in Visual Basic and make the project an ActiveX DLL. Add a reference to ActiveX Data Objects (ADO) by clicking the Project menu and selecting References. Select "Microsoft OLE DB ActiveX Data Objects 1.0 Library" and click OK. Add a new module to the project by selecting the Project menu and clicking Add Module. Select Module and click Open. Place the following code in the (general) (declarations) section of MODULE1.BAS: ' Enter the following Declare statement as one single line: Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long) Type PT Width As Integer Height As Integer End Type Type OBJECTHEADER Signature As Integer HeaderSize As Integer ObjectType As Long NameLen As Integer ClassLen As Integer NameOffset As Integer ClassOFfset As Integer ObjectSize As PT OleInfo As String * 256 End TypePlace the following code in the (general) (declarations) section of CLASS1.CLS: Function DisplayBitmap(ByVal OleField As ADODB.Field) Dim Arr() As Byte Dim ObjHeader As OBJECTHEADER Dim Buffer As String Dim ObjectOffset As Long Dim BitmapOffset As Long Dim BitmapHeaderOffset As Integer Dim ArrBmp() As Byte Dim i As Long 'Resize the array, then fill it with 'the entire contents of the field ReDim Arr(OleField.ActualSize) Arr() = OleField.GetChunk(OleField.ActualSize) 'Copy the first 19 bytes into a variable 'of the OBJECTHEADER user defined type. CopyMemory ObjHeader, Arr(0), 19 'Determine where the Access Header ends. ObjectOffset = ObjHeader.HeaderSize + 1 'Grab enough bytes after the OLE header to get the bitmap header. Buffer = "" For i = ObjectOffset To ObjectOffset + 512 Buffer = Buffer & Chr(Arr(i)) Next i 'Make sure the class of the object is a Paint Brush object If Mid(Buffer, 12, 6) = "PBrush" Then BitmapHeaderOffset = InStr(Buffer, "BM") If BitmapHeaderOffset > 0 Then 'Calculate the beginning of the bitmap BitmapOffset = ObjectOffset + BitmapHeaderOffset - 1 'Move the bitmap into its own array ReDim ArrBmp(UBound(Arr) - BitmapOffset) CopyMemory ArrBmp(0), Arr(BitmapOffset), UBound(Arr) - BitmapOffset + 1 'Return the bitmap DisplayBitmap = ArrBmp End If End If End FunctionRename the Project by selecting the Project menu, and clicking on "Project1 Properties" and type your new name in the "Project Name" field. This example assumes that you named the project "MyProject" and will refer to that name in future steps. Make the project Apartment Model Threaded by selecting the "Unattended Execution" check box. Click OK. Rename the Class in the Property Pane. This example assumes that you named the class "MyClass" and refers to that name in future steps. Compile the DLL by clicking the File menu and selecting "Make MyProject.dll." Create an ASP page named "bitmap.asp" that contains the following code: <%@ LANGUAGE="VBSCRIPT" %> <% ' You need to set up a System DSN named 'NWind' that points to ' the Northwind.mdb database Set DataConn = Server.CreateObject("ADODB.Connection") DataConn.Open "DSN=NWind", "admin", "" Set cmdTemp = Server.CreateObject("ADODB.Command") Set RS = Server.CreateObject("ADODB.Recordset") cmdTemp.CommandText = "SELECT Photo FROM Employees WHERE EmployeeID = 1" cmdTemp.CommandType = 1 Set cmdTemp.ActiveConnection = DataConn RS.Open cmdTemp, , 0, 1 Response.ContentType = "image/bmp" Set Bitmap = Server.CreateObject("MyProject.MyClass") Response.BinaryWrite Bitmap.DisplayBitmap(RS("Photo")) RS.Close %>Create an HTML page named "BitmapTest.htm" that contains the following code: <HTML> <HEAD> <TITLE>Bitmap Test</TITLE> </HEAD> <BODY> <HR> <img src="Bitmap.asp"> <HR> </BODY> </HTML> REFERENCES For additional information, please see the following article in the Microsoft Knowledge Base: ARTICLE-ID: Q173308 TITLE : HOWTO: Displaying Images Stored in a BLOB Field For the latest Knowledge Base artices and other support information on Visual InterDev and Active Server Pages, see the following page on the Microsoft Technical Support site: http://support.microsoft.com/support/vinterdev/ Keywords : AXSFMisc kbcode Technology : kbInetDev Version : WINDOWS:1.0,5.0; WINNT:1.0b Platform : WINDOWS winnt Issue type : kbhowto
--------------------------------------------------------------------------------================================================================================ THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
blob对象的写入和更新是不同的
GetChunk(long lSize)读取数据,AppendChunk写入。
下面是MSDN中的例子,你可以在网上找到VC的:
HOWTO: Retrieving Bitmap from Access and Displaying In Web Page
Last reviewed: December 11, 1997
Article ID: Q175261
The information in this article applies to:
Microsoft Visual InterDev, version 1.0
Microsoft Active Server Pages, version 1.0b
Microsoft Visual Basic Professional and Enterprise Editions for Windows, version 5.0
SUMMARY
This article shows by example how to extract the bitmap photos in the Microsoft Access 97 Northwind.mdb database, and view them from a Web browser using Active Server Pages (ASP). In order to accomplish this task, an ActiveX DLL must be created that strips the Access and OLE headers from the field. This article shows how to create this ActiveX DLL, and how to implement it. MORE INFORMATION
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This article demonstrates how to use Visual Basic to retrieve a bitmap stored in an OLE Object field. Because the definition of OLE object storage is not documented, the following code searches the object's OLE header for characters consistent with the start of the graphic. This method may not work in all circumstances. Be aware that Internet Explorer 3.0 is unable to display true color bitmaps. For this reason, the bitmaps stored in the Access database should be no higher than 256 colors. Step-by-Step Example to Extract the PhotosCreate a new project in Visual Basic and make the project an ActiveX DLL. Add a reference to ActiveX Data Objects (ADO) by clicking the Project menu and selecting References. Select "Microsoft OLE DB ActiveX Data Objects 1.0 Library" and click OK. Add a new module to the project by selecting the Project menu and clicking Add Module. Select Module and click Open. Place the following code in the (general) (declarations) section of MODULE1.BAS:
' Enter the following Declare statement as one single line:
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
(lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long) Type PT
Width As Integer
Height As Integer
End Type Type OBJECTHEADER
Signature As Integer
HeaderSize As Integer
ObjectType As Long
NameLen As Integer
ClassLen As Integer
NameOffset As Integer
ClassOFfset As Integer
ObjectSize As PT
OleInfo As String * 256
End TypePlace the following code in the (general) (declarations) section of CLASS1.CLS:
Function DisplayBitmap(ByVal OleField As ADODB.Field)
Dim Arr() As Byte
Dim ObjHeader As OBJECTHEADER
Dim Buffer As String
Dim ObjectOffset As Long
Dim BitmapOffset As Long
Dim BitmapHeaderOffset As Integer
Dim ArrBmp() As Byte
Dim i As Long 'Resize the array, then fill it with
'the entire contents of the field
ReDim Arr(OleField.ActualSize)
Arr() = OleField.GetChunk(OleField.ActualSize) 'Copy the first 19 bytes into a variable
'of the OBJECTHEADER user defined type.
CopyMemory ObjHeader, Arr(0), 19 'Determine where the Access Header ends.
ObjectOffset = ObjHeader.HeaderSize + 1 'Grab enough bytes after the OLE header to get the bitmap header.
Buffer = ""
For i = ObjectOffset To ObjectOffset + 512
Buffer = Buffer & Chr(Arr(i))
Next i 'Make sure the class of the object is a Paint Brush object
If Mid(Buffer, 12, 6) = "PBrush" Then
BitmapHeaderOffset = InStr(Buffer, "BM")
If BitmapHeaderOffset > 0 Then 'Calculate the beginning of the bitmap
BitmapOffset = ObjectOffset + BitmapHeaderOffset - 1 'Move the bitmap into its own array
ReDim ArrBmp(UBound(Arr) - BitmapOffset)
CopyMemory ArrBmp(0), Arr(BitmapOffset), UBound(Arr) -
BitmapOffset + 1 'Return the bitmap
DisplayBitmap = ArrBmp
End If
End If
End FunctionRename the Project by selecting the Project menu, and clicking on "Project1 Properties" and type your new name in the "Project Name" field. This example assumes that you named the project "MyProject" and will refer to that name in future steps. Make the project Apartment Model Threaded by selecting the "Unattended Execution" check box. Click OK. Rename the Class in the Property Pane. This example assumes that you named the class "MyClass" and refers to that name in future steps. Compile the DLL by clicking the File menu and selecting "Make MyProject.dll." Create an ASP page named "bitmap.asp" that contains the following code:
<%@ LANGUAGE="VBSCRIPT" %>
<%
' You need to set up a System DSN named 'NWind' that points to
' the Northwind.mdb database
Set DataConn = Server.CreateObject("ADODB.Connection")
DataConn.Open "DSN=NWind", "admin", ""
Set cmdTemp = Server.CreateObject("ADODB.Command")
Set RS = Server.CreateObject("ADODB.Recordset")
cmdTemp.CommandText = "SELECT Photo FROM Employees
WHERE EmployeeID = 1"
cmdTemp.CommandType = 1
Set cmdTemp.ActiveConnection = DataConn
RS.Open cmdTemp, , 0, 1
Response.ContentType = "image/bmp"
Set Bitmap = Server.CreateObject("MyProject.MyClass")
Response.BinaryWrite Bitmap.DisplayBitmap(RS("Photo"))
RS.Close
%>Create an HTML page named "BitmapTest.htm" that contains the following code:
<HTML>
<HEAD>
<TITLE>Bitmap Test</TITLE>
</HEAD>
<BODY>
<HR>
<img src="Bitmap.asp">
<HR>
</BODY>
</HTML>
REFERENCES
For additional information, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q173308
TITLE : HOWTO: Displaying Images Stored in a BLOB Field
For the latest Knowledge Base artices and other support information on Visual InterDev and Active Server Pages, see the following page on the Microsoft Technical Support site: http://support.microsoft.com/support/vinterdev/
Keywords : AXSFMisc kbcode
Technology : kbInetDev
Version : WINDOWS:1.0,5.0; WINNT:1.0b
Platform : WINDOWS winnt
Issue type : kbhowto
--------------------------------------------------------------------------------================================================================================
THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
//build by masterz 20050301 with VC2003, Windows 2003, SQLServer 2000.
#include "stdafx.h"
#import "C:\PROGRA~1\COMMON~1\System\ado\msado15.dll" rename( "EOF", "adoEOF" )
struct InitOle
{
InitOle() { ::CoInitialize(NULL); }
~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;
void PrintProviderError(ADODB::_ConnectionPtr pConnection);void print_properties(LPCTSTR name, ADODB::PropertiesPtr Properties)
{
long prop_count = Properties->GetCount();
printf("%s property count = %d\n",name,prop_count);
for(long i=0;i<prop_count;i++)
{
printf("%s property [%d]:%s\n",name,i,(LPCSTR)Properties->GetItem(i)->Name);
}
}
int main(int argc, char* argv[])
{
ADODB::_ConnectionPtr Conn1;
ADODB::_CommandPtr Cmd1;
ADODB::_ParameterPtr oldParam= NULL;
ADODB::_ParameterPtr inParam=NULL;
ADODB::_ParameterPtr blobParam=NULL;
_variant_t vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);
_variant_t vtEmpty2 (DISP_E_PARAMNOTFOUND, VT_ERROR);
//_bstr_t bstrConnect="Provider=OraOLEDB.Oracle;Data Source=orcl;User Id=system;Password=oracle;";
_bstr_t bstrConnect="Driver={SQL Server};Server=localhost;Database=zxg;Uid=sa;Pwd=sa;" ;
//create procedure dbo.insert_update_blob(@fn varchar(9),@filecontent image) as
// if exists (select * from table1 where filename=@fn )
// begin
// update table1 set content=@filecontent where filename=@fn
// end
// else
// begin
// insert table1 (filename,content) values(@fn,@filecontent)
// end
_bstr_t bstrSP("{CALL insert_update_blob(?,?)}" );
try
{
_variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
ADODB::_StreamPtr adostream;
adostream.CreateInstance(_T("ADODB.Stream"));
adostream->Type = ADODB::adTypeBinary;
adostream->Open(varOptional,ADODB::adModeUnknown, ADODB::adOpenStreamUnspecified, _T(""), _T(""));
adostream->LoadFromFile("C:\\masterz\\20041229.rar");
_variant_t vReadTo = adostream->Read(ADODB::adReadAll);
long blob_size = adostream->GetSize();
adostream->Close(); _bstr_t bstrEmpty;
Conn1.CreateInstance( __uuidof( ADODB::Connection ) );
Cmd1.CreateInstance( __uuidof( ADODB::Command ) );
Conn1->ConnectionString = bstrConnect;
Conn1->Open( bstrConnect, bstrEmpty, bstrEmpty, -1 );
Cmd1->ActiveConnection = Conn1;
Cmd1->CommandText = bstrSP;
Cmd1->CommandType = ADODB::adCmdText;
Conn1->Properties->Refresh();
inParam = Cmd1->CreateParameter(_bstr_t("@fn"),ADODB::adChar,ADODB::adParamInput,2,_variant_t( "a" ));
Cmd1->Parameters->Append(inParam);
blobParam = Cmd1->CreateParameter(_bstr_t("@filecontent"),ADODB::adLongVarBinary,ADODB::adParamInput,blob_size,vReadTo);
Cmd1->Parameters->Append(blobParam);
Cmd1->Properties->Refresh();
print_properties("Cmd1",Cmd1->Properties);
Cmd1->Execute(NULL,NULL,ADODB::adExecuteNoRecords);
Conn1->Close();
//select filename,datalength(content) from table1 where datalength(content)>0
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\nCOM error occurred, Source : %s \n Description : %s \n",(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
PrintProviderError(Conn1);
}
printf("\nprogram end\n");
return 0;
}
VOID PrintProviderError(ADODB::_ConnectionPtr pConnection)
{
ADODB::ErrorPtr pErr = NULL;
long nCount = 0;
long i = 0;
if( (pConnection->Errors->Count) > 0)
{
nCount = pConnection->Errors->Count;
for(i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("\n\t Error number: %x\t%s", pErr->Number, (LPCSTR)pErr->Description);
}
}
}