當前位置:首頁 » 操作系統 » vb圖片保存到資料庫

vb圖片保存到資料庫

發布時間: 2025-03-11 02:31:05

1. vb如何將jpg圖片存入資料庫及其讀取方法

VB6 保存和讀取圖片到資料庫

dim stm as ADODB.Stream
dim rs as ADODB.Recordset
sub SavePictureToDB(cn As ADODB.Connection)
'將圖片存入資料庫
On Error GoTo EH
Set stm = New ADODB.Stream
rs.Open "select ImagePath,ImageValue from tbl_Image", cn, adOpenKeyset, adLockOptimistic
CommonDialog1.ShowOpen
Text1.Text = CommonDialog1.FileName

With stm
.Type = adTypeBinary
.Open
.LoadFromFile CommonDialog1.FileName
End With
With rs
.AddNew
.Fields("ImagePath") = Text1.Text
.Fields("ImageValue") = stm.Read
.Update
End With
rs.Close
Set rs = Nothing
Exit Sub
EH: MsgBox Err.Description, vbInformation, "Error"
End Sub

Sub LoadPictureFromDB(cn As ADODB.Connection)
'載資料庫中讀出圖片
On Error GoTo EH
Dim strTemp As String
Set stm = New ADODB.Stream
strTemp = "c:/temp.tmp" '臨時文件,用來保存讀出的圖片
rs.Open "select ImagePath,ImageValue from tbl_image", cn, , , adCmdText
With stm
.Type = adTypeBinary
.Open
.Write rs("ImageValue")
.SaveToFile strTemp, adSaveCreateOverWrite
.Close
End With
Image1.Picture = LoadPicture(strTemp)
Set stm = Nothing
rs.Close
Set rs = Nothing
Exit Sub
EH: MsgBox Err.Description, vbInformation, "Error"
End Sub
也可用FileSystemObject的方式來保存
Dim Sql As String
Dim fs As New FileSystemObject
Sub SavePicture()
Dim sByte() As Byte
Dim bIsNull As Boolean

If fs.FileExists(g_FilePath) Then
Open g_FilePath For Binary Access Read As #1
ReDim sByte(1 To LOF(1))
Get #1, 1, sByte()
Close #1
bIsNull = False
Else
bIsNull = True
End If

Dim rs As New ADODB.Recordset
rs.Open "select empid,empname,pic from emp where empid = '" & Trim(txtEmpId.Text) & "'", cn, adOpenStatic, adLockOptimistic
rs.AddNew
rs!EmpId = txtEmpId.Text
rs!EmpName = txtEmpName.Text
If bIsNull <> True Then
rs!pic = sByte
End If
rs.Update
MsgBox "save data ok!"
txtEmpId.Text = ""
txtEmpName.Text = ""
Set picView.Picture = Nothing
cmdAdd.Enabled = True
End Sub
Sub viewJpg()

Dim TmpFile As String
Dim jByte() As Byte
Sql = "select * from emp"
Set rsViewJpg = New ADODB.Recordset
rsViewJpg.Open Sql, cn, adOpenStatic, adLockOptimistic
rsViewJpg.MoveFirst

If Not rsViewJpg.BOF Then
If Not rsViewJpg.EOF Then
txtEmpId.Text = rsViewJpg!EmpId
txtEmpName.Text = rsViewJpg!EmpName
Set pic.Picture = Nothing
If Not fs.FolderExists(App.Path + "/temp") Then
fs.CreateFolder (App.Path + "/temp")
End If
TmpFile = App.Path + "/Temp/" + rsViewJpg.Fields(0) + ".jpg"
If Not IsNull(rsViewJpg!pic) Then
jByte = rsViewJpg!pic
Open TmpFile For Binary Access Write As #1
Put #1, , jByte
Close #1
pic.Picture = LoadPicture(TmpFile)
End If
End If
End If
End Sub

熱點內容
編譯學堂 發布:2025-03-11 05:31:06 瀏覽:182
蘋果文件夾隱藏 發布:2025-03-11 05:26:42 瀏覽:544
簡訊設置密碼如何關閉 發布:2025-03-11 05:26:39 瀏覽:913
re管理器主文件夾 發布:2025-03-11 05:26:37 瀏覽:712
手機優酷緩存在哪 發布:2025-03-11 05:25:58 瀏覽:432
摩擦引流腳本 發布:2025-03-11 05:17:31 瀏覽:543
中國電信的路由器密碼在哪裡更改 發布:2025-03-11 05:10:40 瀏覽:686
我的世界電腦伺服器必用指令 發布:2025-03-11 05:10:31 瀏覽:631
多集群緩存設計 發布:2025-03-11 05:00:31 瀏覽:459
史式計演算法 發布:2025-03-11 04:55:47 瀏覽:184