Use SQL Server to provide image 2 for your web browser

Source: Internet
Author: User
Tags array exit command line goto integer connect reference
Establish an ActiveX dynamic link library

Using VB 5.0, we want to develop an ActiveX dynamic link library. This dynamic link library allows us to connect to the SQL database, extract the image data,
and save them as files and put them in the catalogue. To establish a dynamic-link library, follow these steps after you open the VB program: first: On the File menu
, select New Project. Next, in the newly created Project dialog box, double-click the ActiveX DLL icon. VB automatically adds one to the project
class, called Class1. Then, in the property container, double-click the Name property and change it to Clsimageloader, keeping the Instancing property as the default
Value MultiUse not move. Finally, in the Project menu, select the Project1 property to open the Project Properties dialog box. Select Universal (General) selection
Xiangka, change the project name to Imageloader. The other fields can be kept intact. Click OK to complete the project for Imageloader
Reset

In the class, 5 let properties, 1 Get Properties (read-only), and two methods are written. The Let property passes the information required by the dynamic link library, and the Get property provides the dynamic
The location of the picture file generated by the State link library. Two methods are used to open a connection to the database to generate a picture file. Table A summarizes the work of these properties and methods
Use. Of course, depending on the needs of your project, you can add more complex features to your class.

Table A: Properties and methods in the ActiveX dynamic link library

Property/Method action

DBName the database name required to open the database connection.

TableName,
Nameofimagecolumn, open the information required by the recordset.
Nameofimagetypecolumn,
Nameofimageidcolumn

ImageFile (Read only) returns the name and location of the picture file to the container.

OpenConnection (method) opens the database connection.

Createtemplateimagefile (method) extracts the image data from the database and saves it to the directory.


There are other private subroutines in the class to initialize classes, terminate classes, open recordsets, create picture files, and handle possible errors.

Note that the Microsoft Active Data Object ADO1.5 (now 2.0) is used to connect to the SQL database. You can also use other data access methods, such as remote data object RDO.
In this project, you must use ADO. To do this, click the Project menu, and then select the Reference (References) menu item. In the reference to
box, select the Microsoft ActiveX Data Objects 1.5 Library, and click OK.

In list B, the complete source code for the class Imageloader is listed. The variable definitions used in the class are given inside the code, and there are other annotations that help you understand the upper and lower
Text

List B: Complete source code for class Imageloader

Private Madoconn as New ADODB. Connection
Private Madorst as New ADODB. Recordset
Private Mstrdbname as String
Private Mstrtablename as String
Private Mstrimagecolumnname as String ' The name of the picture word.
Private mstrimagetypecolumnname as String ' picture Type field name.
Private mstrimageidcolumnname as String ' picture ID field name.
Private Mstrfilename () as String ' array containing the file name and path.
Private Mlngimageid () as Long ' array containing picture IDs
Private Mlngnumberoffiles as Long
Const BLOCKSIZE = 102400


Public Property Let DBName (ByVal Strval as String)
Mstrdbname = Strval
End Property

Public Property Let TableName (ByVal Strval as String)
Mstrtablename = Strval
End Property

Public Property Let Nameofimagecolumn (ByVal Strval as String)
Mstrimagecolumnname = Strval
End Property

Public Property Let Nameofimagetypecolumn (ByVal Strval as String)
Mstrimagetypecolumnname = Strval
End Property

Public Property Let Nameofimageidcolumn (ByVal Strval as String)
Mstrimageidcolumnname = Strval
End Property

Public Property Get ImageFile (ByVal imageID as Integer) as String
Dim Intpos as Integer
Dim Blnfindid as Boolean
Dim I as Integer

Blnfindid = False
For i = 0 to MlngNumberOfFiles-1
If Mlngimageid (i) = imageID Then
intpos = 5 + Len (imageID) + 3
ImageFile = Right (Mstrfilename (i), intpos) ' reformat ' location of file.
Blnfindid = True
End If
Next I

If Blnfindid = False Then
Err.Clear
Err.Raise vbObjectError +, "Get ImageFile", "Can ' t find image file!"
End If

End Property


Public Sub OpenConnection ()
'**********************************************************
' Function: Open a database connection.
'**********************************************************

On Error GoTo Error_Handler
If mstrdbname = "" Then GoTo Error_Handler
If madoconn.state = adStateOpen Then madoconn.close
madoconn.connectionstring = "Driver={sql Server}"; server= (local); Uid=sa; pwd=; Wsid=jia;database= "&
Mstrdbname
Madoconn.connectiontimeout = 15
Madoconn.open
Exit Sub

Error_Handler:
Call HandleError
End Sub

Public Sub Createtempimagefile (ByVal imageID as Integer)
Dim Strimagetype as String
Dim I as Integer
'**********************************************************
' Function: Open recordset, extract binary data, and save data to file. Note The file name is generated with a picture ID.
' Input: Picture ID.
'**********************************************************
If madoconn.state = adstateclosed Then Exit Sub

Call OpenRecordset (imageID)

If madorst.state = adstateclosed Then Exit Sub

On Error GoTo Error_Handler

For i = 0 to MlngNumberOfFiles-1
' Detects if a picture file already exists.
If Mlngimageid (i) = imageID Then Exit Sub
Next I

Mlngnumberoffiles = mlngnumberoffiles + 1
ReDim Preserve mstrfilename (mlngnumberoffiles) ' changes the array size.
ReDim Preserve Mlngimageid (mlngnumberoffiles) ' changes the array size.
Mlngimageid (mlngNumberOfFiles-1) = imageID
Strimagetype = Madorst.fields (mstrimagetypecolumnname) ' Gets the picture type.
Mstrfilename (mlngNumberOfFiles-1) = App.Path & "\images" & _
"\image" & LTrim (STR (imageID)) & "." & Strimagetype ' get picture file name and location.
Call Readfromdb (Madorst.fields (Mstrimagecolumnname), _
Mstrfilename (mlngNumberOfFiles-1), Adorst.fields (Mstrimagecolumnname). ActualSize)
Exit Sub

Error_Handler:
Call HandleError

End Sub

Private Sub OpenRecordset (ByVal imageID as Integer)
Dim SQLText as String
'**********************************************************
' Function: Open a recordset.
' Input: Picture ID.
'**********************************************************

On Error GoTo Error_Handler
If madorst.state = adStateOpen Then madorst.close
SQLText = "Select" & Mstrimagecolumnname & "," & _
Mstrimagetypecolumnname & "from" & Mstrtablename & _
"WHERE" & mstrimageidcolumnname & "=" & imageID

Set madorst.activeconnection = Madoconn
Madorst.open SQLText,, adOpenStatic, adLockReadOnly ' Open recordset.

Exit Sub


Error_Handler:
Call HandleError
End Sub

Private Sub Readfromdb (fld as ADODB. Field, ByVal Diskfile as String, _
Fldsize as Long)
Dim Numblocks as Integer
Dim leftover as Long
Dim bytedata () As byte array for long variable binary data longvarbinary.
Dim strdata As String ' string for long variable binary data LongVarChar.
Dim Destfilenum as Integer
Dim pic as Variant
Dim I as Integer
'**********************************************************
' Function: Extracts binary data and puts the data into a file.
' Input: Picture field, filename/location and data size.
'**********************************************************

If Len (Diskfile) > 0 Then ' Deletes a target file that already exists.
Kill Diskfile
End If

Destfilenum = FreeFile
Open diskfile for Binary as Destfilenum
Numblocks = fldsize \ BLOCKSIZE
leftover = fldsize Mod BLOCKSIZE

Select case FLD. Type
Case Adlongvarbinary ' for picture data type.
Bytedata () = fld. GetChunk (leftover)
Pic = fld. GetChunk (leftover)
Put Destfilenum, Bytedata ()

For i = 1 to Numblocks
Bytedata () = fld. GetChunk (BLOCKSIZE)
Put Destfilenum, Bytedata ()
Next I

Case adLongVarChar ' is used for text data types.
For i = 1 to Numblocks
Strdata = String (BLOCKSIZE, 32)
Strdata = fld. GetChunk (BLOCKSIZE)
Put Destfilenum, strdata.
Next I

Strdata = String (leftover, 32)
Strdata = fld. GetChunk (leftover)
Put Destfilenum, strdata.
Case Else
Err.Clear
Err.Raise vbObjectError +, "Read from DB", "not a Chunk Required column!"
End Select

Close Destfilenum

End Sub

Private Sub HandleError ()
Dim Adoerrs as ADODB. Errors
Dim Errloop as ADODB. Error
Dim Strerror as String
Dim I as Integer
'**********************************************************
' function: to handle possible errors.
'**********************************************************

If madoconn.state = adstateclosed Then GoTo Done
i = 1
Set Adoerrs = madoconn.errors
For the Errloop in Adoerrs ' enumeration error set.
With Errloop
strerror = strerror & vbCrLf & "ADO Error #" &. Number
strerror = strerror & vbCrLf & "Description" &. Description
strerror = strerror & vbCrLf & "Source" &. Source
i = i + 1
End With
Next

Done:
Err.Raise vbObjectError +, "", strerror
End Sub

Private Sub Class_Initialize ()
Mlngnumberoffiles = 0
End Sub

Private Sub Class_Terminate ()
Dim I as Integer
On Error GoTo Error_Handler
If madorst.state = adStateOpen Then madorst.close ' closes a recordset.
If madoconn.state = adStateOpen Then madoconn.close ' closes the connection.
Set Madorst = Nothing
Set Madoconn = Nothing
Exit Sub

Error_Handler:
Call HandleError
End Sub

Error_Handler:
Call HandleError
End Sub


The next step is to compile the dynamic link library. In the File menu, click Establish (Make) ImageLoader.dll. VB will ask to save the bit of the dynamic link library
And then compile the project. To use the ImageLoader.dll dynamic link library in an ASP file, you must register it in the Windows registry. Operation method
Yes, in the NT boot menu, select Run, and then enter the following command line in the input box:

regsvr32 localpath\imageloader
Note that in actual use replace the "LocalPath" in the above command line with the full path where the dynamic link library actually resides. Now, you can do it in a program, like
This dynamic link library is used in ASP files.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.