當前位置:首頁 » 編程語言 » vbsql圖片讀取

vbsql圖片讀取

發布時間: 2022-05-10 10:34:37

⑴ 請問如何用vb讀取sql資料庫中的圖片,我想用一個picturebox顯示資料庫中的圖片,求詳細解釋

這個問題要這樣分析解決:
SQL資料庫里存放的是圖片文件的路徑,如果圖片是存放在本地計算機還好處理,而存放在遠程計算機里的,就比較麻煩.
vb訪問SQL資料庫,可以用控制項或對象,訪問到數據表,讀取對應圖片文件的路徑.
利用picturebox的LoadPicture函數,動態載入圖片

⑵ 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

⑶ vb 如何從SQL中讀取圖片

和直接顯示有區別嗎?只不過多了一步讀資料庫而已吧?

⑷ 在VB中怎樣讀取SQL資料庫中的圖片路徑並顯示圖片

暈倒 這種問題不會有人回答你的
感覺你問這個問題的邏輯十分的有問題
或許你是個新手甚至不知道該從何下手是吧
你這個問題我是無法回答你的了
不過我可以告訴你的是你該問什麼問題
你如果想達到你要的效果你應該提出下列問題

1怎麼樣讀取SQL里資料庫的內容
2使用圖片控制項Image怎麼樣載入指定路徑的圖片

第1個問題我是沒辦法回答你了因為太不具體了這個問題雖然我知道怎麼做
第2個問題就是直接修改控制項屬性你應該會的吧,比如PictureBox1的話就是
PictureBox1.Image="c:\a.bmp"

先從學習SQL資料庫開始吧朋友

⑸ vb.net讀取與存儲sql server 2008圖片

一般圖片存儲與讀取採用數據類型Blob進行存儲。
當然,由於現在資料庫幾乎都是關系型資料庫,所以處理聲音、圖像、視頻等,不能使用對象的方式,所以只能使用特殊的類型Blob進行存儲,但效率比較低。我們在使用時,一般只存路徑(字元串),而不採用Blob存儲。

⑹ VB中怎樣提取及寫入SQL SERVER中的圖片,並顯示在IMAGE中

說思路:
SQL中的圖片都是二進制的
所以你要先圖片在VB中用一個二進制數組給存起來!
IMAGE控制項的參數是要一個文件地址,
所以你還要把這個二進制數組用
OPEN 「FILENAME」 FOR BINARY AS FILENO
PUT---------
把這個數組寫成JPG的文件!
下面就可以在IMAGE中用了!

⑺ vb程序 怎麼把圖片寫入資料庫並讀出來顯示

1.使用存取文件路徑的方法存取圖片
保存文件名與保存其他字元型數據的方法相同,其代碼如下:
Dim photoFilename as String
photoFilename = CommonDialog1.FileName
rs1.AddNew
rs1.Fields("photo")= photoFilename
rs1.Update
2.使用AppendChunk方法將二進制文件存入資料庫中
下面使用AppendChunk方法將不同類型的文件存入到資料庫中,程序主要代碼如下:
Const BLOCKSIZE = 4096
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub SaveToDB(ByRef Fld As ADODB.Field, DiskFile As String)
'定義數據塊數組
Dim byteData() As Byte
'定義數據塊個數
Dim NumBlocks As Long
Dim FileLength As Long
'定義剩餘位元組長度
Dim LeftOver As Long
Dim SourceFile As Long
Dim i As Long
'判斷文件是否存在
If Dir(DiskFile) <> "" Then
SourceFile = FreeFile
'打開二進制文件
Open DiskFile For Binary Access Read As SourceFile
FileLength = LOF(SourceFile)
'判斷文件是否空
If FileLength = 0 Then
Close SourceFile
MsgBox DiskFile & "文件無內容,請重新指定文件!", vbExclamation, "注意"
Else
'得到數據塊的個數
NumBlocks = FileLength \ BLOCKSIZE
'得到剩餘位元組數
LeftOver = FileLength Mod BLOCKSIZE
Fld.Value = Null
ReDim byteData(BLOCKSIZE)
For i = 1 To NumBlocks
Get SourceFile, , byteData()
'用Appendchunk方法將byteData()數據寫入FLD
Fld.AppendChunk byteData()
DoEvents
Next i
'將剩餘數據寫入FLD
ReDim byteData(LeftOver)
Get SourceFile, , byteData()
Fld.AppendChunk byteData()
Close SourceFile
End If
Else
MsgBox "文件不存在,請重新指定文件!", vbExclamation, "注意"
End If
End Sub
3.使用Stream對象將二進制文件存入資料庫
在ADO2.5以上版本中提供了一個Stream對象,該對象的引入大大簡化了二進制欄位的存取操作,但使用前需要引用ADO 2.5Library以上的版本
下面使用Stream對象將各種類型的文件存入到資料庫中,程序代碼如下:
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim mst As New ADODB.Stream
Private Sub cmdSave_Click()
rs.Open "select * from 文件表", cn, adOpenDynamic, adLockOptimistic
rs.AddNew
rs.Fields("名稱") = Text1.Text
mst.Type = adTypeBinary
mst.Open
If Text2.Text <> "" Then mst.LoadFromFile Text2.Text
rs.Fields("文件") = mst.Read
rs.Update
mst.Close
rs.Close
End Sub
Private Sub Command1_Click()
On Error GoTo Err
CommonDialog1.Filter = "所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
Text2.Text = CommonDialog1.FileName
Exit Sub
Err:
MsgBox Err.Description
End Sub
Private Sub Form_Load()
Dim cnnstr As String
cnnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db_wjgl.mdb;Persist Security Info=False"
cn.Open cnnstr
Adodc1.ConnectionString = cnnstr
Set Picture1.DataSource = Adodc1
Picture1.DataField = "文件"
End Sub

⑻ 如何用vb向sql資料庫存取照片 最好有事例 謝謝

在VB中存取資料庫中的圖片
2001-07-05·
·史美康
··vbeden.com
一、
資料庫的設計
資料庫可以採用微軟的Access97或者SQL
Server來進行,首先新建一張表,取名為Table,添加三個欄位,分別是:姓名
Char型(SQL
Server中)文本型(Access中);編號Char型(SQL
Server中)文本型(Access中);照片image型(SQL
Server中)OLE對象(Access中),設計好後存檔。為了可以進行遠程調用,我們採用ODBC的方法進行,雙擊打開控制面板里的ODBC數據源,點「系統DSN」選項卡,按「添加」按鈕選擇對應的數據源驅動程序Access的*.mdb或者SQL
Server,依照添加向導加添加數據源,下面就可以開始程序的編寫了。
二、
程序的編寫
運行VB,新建一個工程。本程序採用ADO控制項和動態鏈接庫訪問資料庫,需要加入ADO的運行庫,單擊「工程\引用」菜單,出現引用對話框,選擇Microsoft
ActiveX
Data
Objects2.0
Library並確定。
添加一個Form,四個Label控制項,兩個TextBox控制項,一個PictureBox控制項,一個ADODC控制項,三個CommandButton控制項,一個CommandDialog控制項,如果ADODC和CommandDialog控制項沒有出現在工具框上,請單擊菜單「工程\部件」。點「控制項」選項卡,在其中選中Microsoft
ADO
Data
Control
6.0(OLEDB)和Microsoft
Common
Dialog
Control
6.0兩項按「確定」按鈕。
下面是以上各個控制項的一些屬性:
Form1.MaxButton=False
Label1.Caption=姓名:
Label2.Caption=編號:
Label3.Name=
ResName
Label3.BackColor=
&H80000009&
Label3.BorderStyle=1-Fixed
Single
Label3.DataField=姓名
Label3.DataSource=
AdoCtr
Label4.Name=
ResNumb
Label4.BackColor=
&H80000009&
Label4.BorderStyle=1-Fixed
Single
Label4.DataField=編號
Label4.DataSource=
AdoCtr
Text1.Name=
Names
Text2.Name=
Numb
CommonDialog1.Name=
CDlg
Adodc1.Name=AdoCtr
CommonButton1.Name=PreView
CommonButton1.Caption=預覽
CommonButton2.Name=Save
CommonButton2.Caption=保存
CommonButton3.Name=
Update
CommonButton3.Caption=更新
PictureBox1.Name=
PicBox
PictureBox1.AutoSize=False
PictureBox1.AutoRedraw=False
PictureBox1.DataField=照片
PictureBox1.DataSource=AdpCtr
下面是程序代碼:
′此工程需有Microsoft
ActiveX
Data
Object
2.1
Library(msado15.dll)
Dim
Constr
As
String
′ODBC路徑
Dim
FileName
As
String
′圖片文件名
Const
BLOCKSIZE
=
4096
′每次讀寫塊的大小
Dim
ADOCon
As
New
ADODB.Connection
′ADODB
Connection對象
Dim
ADORst
As
New
ADODB.Recordset
′ADODB
Recordset
對象
Dim
ADOFld
As
ADODB.Field
′ADODB
Field
對象
------------------------
Private
Sub
SaveToDB(ByRef
Fld
As
ADODB.Field,
DiskFile
As
String)
Dim
byteData()
As
Byte
′定義數據塊數組
Dim
NumBlocks
As
Long
′定義數據塊個數
Dim
FileLength
As
Long
′標識文件長度
Dim
LeftOver
As
Long′定義剩餘位元組長度
Dim
SourceFile
As
Long
′定義自由文件號
Dim
i
As
Long
′定義循環變數
SourceFile
=
FreeFile
′提供一個尚未使用的文件號
Open
DiskFile
For
Binary
Access
Read
As
SourceFile
′打開文件
FileLength
=
LOF(SourceFile)
′得到文件長度
If
FileLength
=
0
Then
′判斷文件是否存在
Close
SourceFile
MsgBox
DiskFile
&








!〃
Else
NumBlocks
=
FileLength
\
BLOCKSIZE
′得到數據塊的個數
LeftOver
=
FileLength
Mod
BLOCKSIZE
′得到剩餘位元組數
Fld.Value
=
Null
ReDim
byteData(BLOCKSIZE)
′重新定義數據塊的大小
For
i
=
1
To
NumBlocks
Get
SourceFile,
,
byteData()

讀到內存塊中
Fld.AppendChunk
byteData()
′寫入FLD
Next
i
ReDim
byteData(LeftOver)
′重新定義數據塊的大小
Get
SourceFile,
,
byteData()
′讀到內存塊中
Fld.AppendChunk
byteData()
′寫入FLD
Close
SourceFile
′關閉源文件
End
If
End
Sub
----------------------
Private
Sub
Form_Load()
Constr
=
〃DSN=image〃
′定義ODBC連接
ADOCon.Open
Constr
′創建一個連接
ADORst.Open
〃table〃,
ADOCon,
adOpenDynamic,
adLockOptimistic
′打開一個ADO動態集
表名為table
Set
AdoCtr.Recordset
=
ADORst
′將動態集賦給ADO控制項
End
Sub
----------------------
Private
Sub
Form_Unload(Cancel
As
Integer)
′記得關閉打開的數據集,釋放資源
ADORst.Close
ADOCon.Close
Set
ADORst
=
Nothing
Set
ADOCon
=
Nothing
End
Sub
----------------------
Private
Sub
PreView_Click()
′顯示打開文件的公用對話框,選擇需要加入資料庫的圖片
CDlg.Filter
=
〃點陣圖(*.bmp)|*.bmp〃
CDlg.ShowOpen
FileName
=
CDlg.FileName
PicBox.Picture
=
LoadPicture(FileName)
′預覽圖片
End
Sub
----------------------
Private
Sub
Save_Click()
ADORst.AddNew
′新增紀錄
ADORst(〃姓名〃).Value
=
Names.Text
′給動態集的第一個欄位賦值
ADORst(〃編號〃).Value
=
Numb.Text
′給動態集的第二個欄位賦值
Set
ADOFld
=
ADORst(〃照片〃)
′給ADODB.Field對象賦值
Call
SaveToDB(ADOFld,
FileName)
′調用子程序,給第三個欄位(image)賦值
ADORst.Update
End
Sub
----------------------
Private
Sub
Update_Click()
′重新打開紀錄集,刷新紀錄
ADORst.Close
ADOCon.Close
Set
ADORst
=
Nothing
Set
ADOCon
=
Nothing
ADOCon.Open
Constr
ADORst.Open
〃table〃,
ADOCon,
adOpenDynamic,
adLockOptimistic
Set
AdoCtr.Recordset
=
ADORst
End
Sub

⑼ 如何用VB將圖片導入到SQL資料庫

用stream組件就可以了,給你個示例

保存文件到資料庫中
Sub s_SaveFile()
Dim iStm As ADODB.Stream
Dim iRe As ADODB.Recordset
Dim iConcstr As String

'讀取文件到內容
Set iStm = New ADODB.Stream
With iStm
.Type = adTypeBinary '二進制模式
.Open
.LoadFromFile App.Path + "\test.jpg"
End With

'打開保存文件的表
Set iRe = New ADODB.Recordset
With iRe
.Open "select * from img", iConc, 1, 3
.AddNew '新增一條記錄
.Fields("photo") = iStm.Read
.Update
End With

熱點內容
鬥地主源碼開發 發布:2025-05-11 02:24:07 瀏覽:364
雲伺服器怎麼設置攻擊 發布:2025-05-11 02:22:09 瀏覽:824
python嵌套for循環 發布:2025-05-11 01:51:44 瀏覽:227
安卓怎麼取消後台限制 發布:2025-05-11 01:45:45 瀏覽:257
一鍵搭建sk5伺服器 發布:2025-05-11 01:40:09 瀏覽:513
鴻業acs加密鎖模擬器 發布:2025-05-11 01:38:49 瀏覽:937
神廟逃亡2安卓版怎麼玩 發布:2025-05-11 01:38:05 瀏覽:161
凱傑都什麼配置 發布:2025-05-11 01:38:04 瀏覽:471
php微信開源系統源碼 發布:2025-05-11 01:37:54 瀏覽:813
pythonfor多個參數 發布:2025-05-11 01:12:32 瀏覽:74