用ado怎样把图象存入到sql server中,急!急!急1

yhp 2000-11-24 04:36:00
我现在做一个系统,需要把图象(bmp,jpg,gif)存入到sql server中的image字段中。
用ado怎么实现。
现在好急啊!快帮帮我!
...全文
123 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Edward 2000-11-24
  • 打赏
  • 举报
回复
Option Explicit

Dim byteArray() As Byte

Private Sub cmdBrowse_Click()
Me.CommonDialog1.ShowOpen
If Len(Me.CommonDialog1.FileName) > 0 Then
Me.txtPath.Text = Me.CommonDialog1.FileName
End If
End Sub

Private Sub cmdDownload_Click()
Dim oConn As ADODB.Connection
Dim oCmd As ADODB.Command
Dim oRS As ADODB.Recordset
Dim strPath As String
Dim i As Long
Dim fldPhoto As ADODB.Field
Dim varData() As Byte

On Error GoTo errHandler
i = InputBox("Enter record ID")

Set oConn = New ADODB.Connection
oConn.Open "Driver={SQL Server};Server=websvr;uid=sa;password=MSSQLSERVER;Database=demo"
Set oRS = New ADODB.Recordset
oRS.Open "SELECT * FROM blobtest WHERE id = " & i, oConn, adOpenKeyset, adLockOptimistic

Set fldPhoto = oRS("photo")
varData = fldPhoto.GetChunk(fldPhoto.ActualSize)

oRS.Close
oConn.Close
Set oRS = Nothing
Set oConn = Nothing

strPath = "c:\test.jpg"
SaveToFile strPath, varData
Image1.Picture = LoadPicture(strPath)
Exit Sub

errHandler:
MsgBox "Oops! We had an error." & vbCrLf & "Error &H" & Hex(Err) & " - " & vbCrLf & Err.Description, vbInformation
Exit Sub
End Sub

Private Sub cmdUpload_Click()
Dim oConn As ADODB.Connection
Dim oCmd As ADODB.Command
Dim oRS As ADODB.Recordset
Dim strPath As String

strPath = txtPath.Text
If Len(Dir(strPath)) = 0 Then
txtPath.SetFocus
Exit Sub
End If

On Error GoTo errHandler

Set oConn = New ADODB.Connection
oConn.Open "Driver={SQL Server};Server=websvr;uid=sa;password=MSSQLSERVER;Database=demo"
Set oRS = New ADODB.Recordset
oRS.Open "blobtest", oConn, adOpenKeyset, adLockOptimistic
oRS.AddNew
If GetFileData(strPath) Then
oRS("photo").AppendChunk byteArray
oRS.Update
End If

oRS.Close
oConn.Close
Set oRS = Nothing
Set oConn = Nothing

Exit Sub

errHandler:
MsgBox "Oops! We had an error." & vbCrLf & "Error &H" & Hex(Err) & " - " & vbCrLf & Err.Description, vbInformation
Exit Sub
End Sub

Function GetFileData(ByVal strPath As String) As Boolean
Dim hFileHandle

ReDim byteArray(FileLen(strPath))
hFileHandle = FreeFile
Open strPath For Binary Access Read Lock Write As #hFileHandle
Get #hFileHandle, , byteArray
GetFileData = True
Close #hFileHandle
End Function

Function SaveToFile(ByVal strPath As String, varData() As Byte) As Boolean
Dim hFileHandle As Long

hFileHandle = FreeFile
Open strPath For Binary Access Write Lock Write As #hFileHandle
Put #hFileHandle, , varData
Close #hFileHandle
SaveToFile = True
End Function

7,786

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧