日期:2011-05-02 15:22:00 来源:本站整理
用SQL Server为Web浏览器供应图象(三)(转)[MSSQL防范]
本文“用SQL Server为Web浏览器供应图象(三)(转)[MSSQL防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
成立ActiveX 动态链接库
利用 VB 5.0,我们要开辟一个ActiveX 动态链接库.这个动态链接库让我们可以衔接到SQL数据库,提取图片数据,并把它们保存成文件,放在目录里.要成立动态链接库,请在翻开VB程序之后履行下列步骤:首先:在文件(File)菜单里,挑选新建项目(New Project).接着,在新成立项目对话框里,双击ActiveX DLL 图标.VB自动地向项目里增添一个类,叫做Class1.然后,在属性容器里,双击名称(Name)属性,把它改成clsImageLoader,保持instancing 属性为缺省值MultiUse不动.最后,在项目(Project)菜单里,挑选Project1 属性,翻开项目属性对话框.挑选通用(General)选项卡,把项目名称改成ImageLoader.别的字段可以不管,保持原样.单击肯定(OK)完成对ImageLoader 这个工程的设置.
在类里,写入了5个Let属性,1个Get属性(只读)和两个办法.Let属性传送动态链接库需求的信息,Get属性供应动态链接库生成的图片文件的位置.两个办法用来翻开到数据库的衔接,以生成图片文件.表 A 总结了这些属性和办法的作用.当然,按照项目的需求,可以在类里加入更复杂的功效.
表A: 在ActiveX动态链接库里的属性和办法
属性/办法 作用
DbName 翻开数据库衔接所需的数据库名称.
TableName,
NameOfImageColumn, 翻开记录集所需求的信息.
NameOfImageTypeColumn,
NameOfImageIdColumn
ImageFile (只读) 把图片文件的名称和位置返回给容器.
OpenConnection (办法) 翻开数据库衔接.
CreateTemplateImageFile (办法) 从数据库提取图片数据,并把它保存到目录里.
在类里还有别的的私有子程序,用来初始化类、终止类、翻开记录集、成立图片文件,以及处理大概的错误.
注意微软活动数据对象ADO1.5(目前为2.0)被用来衔接SQL数据库.也可以利用别的数据拜候办法,比方远程数据对象RDO.在这个项目里,必须利用ADO.操作办法是,单击项目(Project)菜单,然后挑选引用(References)菜单 项.在引用对话框里,选中微软ActiveX Data Objects 1.5 库,然后单击肯定(OK).
在列表 B里,列出了类ImageLoader 完好源代码.类里利用的变量定义都在代码内部给出,还有别的注释,帮忙理解上下文.
列表 B:类 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 '图片字的名称.
Private mstrImageTypeColumnName As String '图片范例字段的名称.
Private mstrImageIdColumnName As String '图片ID字段的名称.
Private mstrFileName() As String '数组,里面包含文件名和途径.
Private mlngImageId() As Long '数组,里面包含图片ID
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 the location of file.
blnFindId = True
End If
Next i
If blnFindId = False Then
Err.Clear
Err.Raise vbObjectError + 23, "Get ImageFile", "Can't find image file!"
End If
End Property
Public Sub OpenConnection()
'**********************************************************
'作用:翻开数据库衔接.
'**********************************************************
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
'**********************************************************
'作用:翻开记录集,提取二进制数据,并把数据存入文件.注意文件名利用图片ID生成.
'输入:图片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
'检测图片文件能否已经存在.
If mlngImageId(i) = ImageId Then Exit Sub
Next i
mlngNumberOfFiles = mlngNumberOfFiles + 1
ReDim Preserve mstrFileName(mlngNumberOfFiles) '改变数组大小.
ReDim Preserve mlngImageId(mlngNumberOfFiles) '改变数组大小.
mlngImageId(mlngNumberOfFiles - 1) = ImageId
strImageType = mAdoRst.Fields(mstrImageTypeColumnName) ' 获得图片范例.
mstrFileName(mlngNumberOfFiles - 1) = App.Path & "\images" & _
"\image" & LTrim(Str(ImageId)) & "." & strImageType '获得图片文件名称和位置.
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
'**********************************************************
'作用:翻开记录集.
'输入:图片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 '字节数组,用于长的可变二进制数据LongVarBinary.
Dim strData As String '字符串,用于长的可变二进制数据LongVarChar.
Dim DestFileNum As Integer
Dim pic As Variant
Dim i As Integer
'**********************************************************
'作用:提取二进制数据并把数据放入文件.
'输入:图片字段,文件名/位置和数据尺寸.
'**********************************************************
If Len(Dir(DiskFile)) > 0 Then '删除已经存在的目标文件.
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 '用于图片数据范例.
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 '用于文本数据范例.
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 + 22, "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
'**********************************************************
'作用:处理大概的错误.
'**********************************************************
If mAdoConn.State = adStateClosed Then GoTo Done
i = 1
Set adoErrs = mAdoConn.Errors
For Each errLoop In adoErrs '列举错误集.
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 + 21, "", 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 '关闭记录集.
If mAdoConn.State = adStateOpen Then mAdoConn.Close '关闭衔接.
Set mAdoRst = Nothing
Set mAdoConn = Nothing
Exit Sub
Error_handler:
Call HandleError
End Sub
Error_handler:
Call HandleError
End Sub
下一步是编译动态链接库.在文件(File)菜单里,单击成立(Make) ImageLoader.dll.VB会询问保存动态链接库的位置,然后就编译项目.在要ASP文件里利用ImageLoader.dll这个动态链接库,必须在Windows注册表里注册它.操作办法是,在NT的启动菜单里,挑选运行(Run),然后在输入框里输入下面的号令行:
regsvr32 localpath\ImageLoader
注意,实际利用时要用动态链接库实际所在的完好途径替换上面号令行里的"localpath".目前,便可以在程序里,比方ASP文件里利用这个动态链接库了
以上是“用SQL Server为Web浏览器供应图象(三)(转)[MSSQL防范]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论