众所周知,MDB数据库的通用类型和二进制类型字段都可以存取图片,但这样做会使数据库的体积庞大,降低数据库的存取效率。 笔者思考出数据库中存取图片的另一种方法,希望与爱好者们分享。 一、实现思路: 用户添加或修改图片时控制使用COMMONDIALOG的SHOWOPEN对话框,然后记录通用对话框的FILENAME到变量A,再使用APP.PATH找到到特定目录下的该图片文件,改变窗体上放置图片控件的属性即可显示。数据库中只使用一文本型字段记录下变量A,图片文件保存在程序目录下,此法可同时使用GIF、JPG和BMP等等图像文件。 二、实现方法: 1、在窗体上建立IMAGE控件,名为IMAGE1。 2、用DATA控件得到数据源表。 3、建立“更改图片”按钮COMMAND1,其代码为: Private Sub Command1_Click() CommonDialog1.FileName = "" CommonDialog1.Filter = "文件bmp|*.bmp|文件JPG|*.JPG|文件GIF|*.GIF" CommonDialog1.ShowOpen Dim a As String a = CommonDialog1.FileTitle '得到不含路径文件名 If a <> "" Then Image11.Picture = LoadPicture(App.Path & "\bmp\" & a) '\bmp\是我用于存取图片的本系统文件夹下的一个子文件夹。 Data1.Recordset.Edit Data1.Recordset.Fields("tp") = a 'TP是存取图片名称的字段。 Data1.Recordset.Update End If End Sub
软件开发 如何用VB6读写数据库中的图片 很多人问关于VB6读写数据库中的图片的问题,在此有一例,希有所启发。1,以人名和相关图片为例说明,数据库为Access,有如下字段:Name char,picture OLE object,FileLength Number。当为ms sql时,将picture改为lob即可。2,示例包含control:commom dialog,picture,listbox。源码如下: Option ExplicitPrivate Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long Private Const MAX_PATH = 260Private m_DBConn As ADODB.ConnectionPrivate Const BLOCK_SIZE = 10000 ' Return a temporary file name. Private Function TemporaryFileName() As String Dim temp_path As String Dim temp_file As String Dim length As Long' Get the temporary file path. temp_path = Space$(MAX_PATH) length = GetTempPath(MAX_PATH, temp_path) temp_path = Left$(temp_path, length)' Get the file name. temp_file = Space$(MAX_PATH) GetTempFileName temp_path, "per", 0, temp_file TemporaryFileName = Left$(temp_file, InStr(temp_file, Chr$(0)) - 1) End Function Private Sub Form_Load() Dim db_file As String Dim rs As ADODB.Recordset' Get the database file name. db_file = App.Path If Right$(db_file, 1) <> "\" Then db_file = db_file & "\" db_file = db_file & "dbpict.mdb"' Open the database connection. Set m_DBConn = New ADODB.Connection m_DBConn.Open _ "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & db_file & ";" & _ "Persist Security Info=False"' Get the list of people. Set rs = m_DBConn.Execute("SELECT Name FROM People ORDER BY Name", , adCmdText) Do While Not rs.EOF lstPeople.AddItem rs!Name rs.MoveNext Looprs.Close Set rs = Nothing End Sub Private Sub Form_Resize() lstPeople.Height = ScaleHeight End Sub ' Display the clicked person. Private Sub lstPeople_Click() Dim rs As ADODB.Recordset Dim bytes() As Byte Dim file_name As String Dim file_num As Integer Dim file_length As Long Dim num_blocks As Long Dim left_over As Long Dim block_num As Long Dim hgt As SinglepicPerson.Visible = False Screen.MousePointer = vbHourglass DoEvents' Get the record. Set rs = m_DBConn.Execute("SELECT * FROM People WHERE Name='" & _ lstPeople.Text & "'", , adCmdText) If rs.EOF Then Exit Sub' Get a temporary file name. file_name = TemporaryFileName()' Open the file. file_num = FreeFile Open file_name For Binary As #file_num' Copy the data into the file. file_length = rs!FileLength num_blocks = file_length / BLOCK_SIZE left_over = file_length Mod BLOCK_SIZEFor block_num = 1 To num_blocks bytes() = rs!Picture.GetChunk(BLOCK_SIZE) Put #file_num, , bytes() Next block_numIf left_over > 0 Then bytes() = rs!Picture.GetChunk(left_over) Put #file_num, , bytes() End IfClose #file_num' Display the picture file. picPerson.Picture = LoadPicture(file_name) picPerson.Visible = TrueWidth = picPerson.Left + picPerson.Width + Width - ScaleWidth hgt = picPerson.Top + picPerson.Height + Height - ScaleHeight If hgt < 1440 Then hgt = 1440 Height = hgtKill file_name Screen.MousePointer = vbDefault End SubPrivate Sub mnuRecordAdd_Click() Dim rs As ADODB.Recordset Dim person_name As String Dim file_num As String Dim file_length As String Dim bytes() As Byte Dim num_blocks As Long Dim left_over As Long Dim block_num As Longperson_name = InputBox("Name") If Len(person_name) = 0 Then Exit SubdlgPicture.Flags = _ cdlOFNFileMustExist Or _ cdlOFNHideReadOnly Or _ cdlOFNExplorer dlgPicture.CancelError = True dlgPicture.Filter = "Graphics Files|*.bmp;*.ico;*.jpg;*.gif"On Error Resume Next dlgPicture.ShowOpen If Err.Number = cdlCancel Then Exit Sub ElseIf Err.Number <> 0 Then MsgBox "Error " & Format$(Err.Number) & _ " selecting file." & vbCrLf & Err.Description Exit Sub End If' Open the picture file. file_num = FreeFile Open dlgPicture.FileName For Binary Access Read As #file_numfile_length = LOF(file_num) If file_length > 0 Then num_blocks = file_length / BLOCK_SIZE left_over = file_length Mod BLOCK_SIZESet rs = New ADODB.Recordset rs.CursorType = adOpenKeyset rs.LockType = adLockOptimistic rs.Open "Select Name, Picture, FileLength FROM People", m_DBConnrs.AddNew rs!Name = person_name rs!FileLength = file_lengthReDim bytes(BLOCK_SIZE) For block_num = 1 To num_blocks Get #file_num, , bytes() rs!Picture.AppendChunk bytes() Next block_numIf left_over > 0 Then ReDim bytes(left_over) Get #file_num, , bytes() rs!Picture.AppendChunk bytes() End Ifrs.Update Close #file_numlstPeople.AddItem person_name lstPeople.Text = person_name End If End Sub
2002-11-15 14:51 转自: 天极网
众所周知,MDB数据库的通用类型和二进制类型字段都可以存取图片,但这样做会使数据库的体积庞大,降低数据库的存取效率。 笔者思考出数据库中存取图片的另一种方法,希望与爱好者们分享。 一、实现思路: 用户添加或修改图片时控制使用COMMONDIALOG的SHOWOPEN对话框,然后记录通用对话框的FILENAME到变量A,再使用APP.PATH找到到特定目录下的该图片文件,改变窗体上放置图片控件的属性即可显示。数据库中只使用一文本型字段记录下变量A,图片文件保存在程序目录下,此法可同时使用GIF、JPG和BMP等等图像文件。 二、实现方法: 1、在窗体上建立IMAGE控件,名为IMAGE1。 2、用DATA控件得到数据源表。 3、建立“更改图片”按钮COMMAND1,其代码为: Private Sub Command1_Click() CommonDialog1.FileName = "" CommonDialog1.Filter = "文件bmp|*.bmp|文件JPG|*.JPG|文件GIF|*.GIF" CommonDialog1.ShowOpen Dim a As String a = CommonDialog1.FileTitle '得到不含路径文件名 If a <> "" Then Image11.Picture = LoadPicture(App.Path & "\bmp\" & a) '\bmp\是我用于存取图片的本系统文件夹下的一个子文件夹。 Data1.Recordset.Edit Data1.Recordset.Fields("tp") = a 'TP是存取图片名称的字段。 Data1.Recordset.Update End If End Sub
我这里提供一个用PHP操纵blob字段的例子给你,希望能有所帮助!这个例子是把用户上传的图片文件存放到BLOB中。
假设有一个表,结构如下:
CREATE TABLE PICTURES (
ID NUMBER,
DESCRIPTION VARCHAR2(100),
PICTURE BLOB
);
然后是用来处理数据的PHP程序代码。
〈?php //建立Oracle数据库连接
$conn = OCILogon($user, $password, $SID);
//提交SQL语句给Oracle
//在这里要注意的两点:一是用EMPTY_BLOB()函数。这是Oracle的内部函数,返回一个LOB的定位符。在插入LOB时,只能用这个办法先生成一个空的LOB定位符,然后对这个定位符进行操作。EMPTY_BLOB()函数是针对BLOB类型的,对应于CLOB的是EMPTY_CLOB()。二是RETURNING后面的部分,把picture返回,让PHP的OCI函数能够处理。
$stmt = OCIParse($conn,"INSERT INTO PICTURES (id, description, picture)
VALUES (pic_seq.NEXTVAL, '$description', EMPTY_BLOB()) RETURNING picture INTO :PICTURE");
//生成一个本地LOB对象的描述符。注意函数的第二个参数:OCI_D_LOB,表示生成一个LOB对象。其它可能的还有OCI_D_FILE和OCI_D_ROWID,分别对应于BFILE和ROWID对象。
$lob = OCINewDescriptor($conn, OCI_D_LOB);
//将生成的LOB对象绑定到前面SQL语句返回的定位符上。
OCIBindByName($stmt, ':PICTURE', &$lob, -1, OCI_B_BLOB);
OCIExecute($stmt);
//向LOB对象中存入数据。因为这里的源数据是一个文件,所以直接用LOB对象的savefile()方法。LOB对象的其它方法还有:save()和load(),分别用来保存和取出数据。但BFILE类型只有一个方法就是save()
if($lob-〉savefile($lob_upload)){
OCICommit($conn);
echo "上传成功〈br〉";
}else{
echo "上传失败〈br〉";
}
//释放LOB对象
OCIFreeDesc($lob);
OCIFreeStatement($stmt);
OCILogoff($conn); ?〉
如何用VB6读写数据库中的图片
很多人问关于VB6读写数据库中的图片的问题,在此有一例,希有所启发。1,以人名和相关图片为例说明,数据库为Access,有如下字段:Name char,picture OLE object,FileLength Number。当为ms sql时,将picture改为lob即可。2,示例包含control:commom dialog,picture,listbox。源码如下:
Option ExplicitPrivate Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath
As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As
String) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As
Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH = 260Private m_DBConn As ADODB.ConnectionPrivate Const BLOCK_SIZE = 10000
' Return a temporary file name.
Private Function TemporaryFileName() As String
Dim temp_path As String
Dim temp_file As String
Dim length As Long' Get the temporary file path.
temp_path = Space$(MAX_PATH)
length = GetTempPath(MAX_PATH, temp_path)
temp_path = Left$(temp_path, length)' Get the file name.
temp_file = Space$(MAX_PATH)
GetTempFileName temp_path, "per", 0, temp_file
TemporaryFileName = Left$(temp_file, InStr(temp_file, Chr$(0)) - 1)
End Function
Private Sub Form_Load()
Dim db_file As String
Dim rs As ADODB.Recordset' Get the database file name.
db_file = App.Path
If Right$(db_file, 1) <> "\" Then db_file = db_file & "\"
db_file = db_file & "dbpict.mdb"' Open the database connection.
Set m_DBConn = New ADODB.Connection
m_DBConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & db_file & ";" & _
"Persist Security Info=False"' Get the list of people.
Set rs = m_DBConn.Execute("SELECT Name FROM People ORDER BY Name", , adCmdText)
Do While Not rs.EOF
lstPeople.AddItem rs!Name
rs.MoveNext
Looprs.Close
Set rs = Nothing
End Sub
Private Sub Form_Resize()
lstPeople.Height = ScaleHeight
End Sub
' Display the clicked person.
Private Sub lstPeople_Click()
Dim rs As ADODB.Recordset
Dim bytes() As Byte
Dim file_name As String
Dim file_num As Integer
Dim file_length As Long
Dim num_blocks As Long
Dim left_over As Long
Dim block_num As Long
Dim hgt As SinglepicPerson.Visible = False
Screen.MousePointer = vbHourglass
DoEvents' Get the record.
Set rs = m_DBConn.Execute("SELECT * FROM People WHERE Name='" & _
lstPeople.Text & "'", , adCmdText)
If rs.EOF Then Exit Sub' Get a temporary file name.
file_name = TemporaryFileName()' Open the file.
file_num = FreeFile
Open file_name For Binary As #file_num' Copy the data into the file.
file_length = rs!FileLength
num_blocks = file_length / BLOCK_SIZE
left_over = file_length Mod BLOCK_SIZEFor block_num = 1 To num_blocks
bytes() = rs!Picture.GetChunk(BLOCK_SIZE)
Put #file_num, , bytes()
Next block_numIf left_over > 0 Then
bytes() = rs!Picture.GetChunk(left_over)
Put #file_num, , bytes()
End IfClose #file_num' Display the picture file.
picPerson.Picture = LoadPicture(file_name)
picPerson.Visible = TrueWidth = picPerson.Left + picPerson.Width + Width - ScaleWidth
hgt = picPerson.Top + picPerson.Height + Height - ScaleHeight
If hgt < 1440 Then hgt = 1440
Height = hgtKill file_name
Screen.MousePointer = vbDefault
End SubPrivate Sub mnuRecordAdd_Click()
Dim rs As ADODB.Recordset
Dim person_name As String
Dim file_num As String
Dim file_length As String
Dim bytes() As Byte
Dim num_blocks As Long
Dim left_over As Long
Dim block_num As Longperson_name = InputBox("Name")
If Len(person_name) = 0 Then Exit SubdlgPicture.Flags = _
cdlOFNFileMustExist Or _
cdlOFNHideReadOnly Or _
cdlOFNExplorer
dlgPicture.CancelError = True
dlgPicture.Filter = "Graphics Files|*.bmp;*.ico;*.jpg;*.gif"On Error Resume Next
dlgPicture.ShowOpen
If Err.Number = cdlCancel Then
Exit Sub
ElseIf Err.Number <> 0 Then
MsgBox "Error " & Format$(Err.Number) & _
" selecting file." & vbCrLf & Err.Description
Exit Sub
End If' Open the picture file.
file_num = FreeFile
Open dlgPicture.FileName For Binary Access Read As #file_numfile_length = LOF(file_num)
If file_length > 0 Then
num_blocks = file_length / BLOCK_SIZE
left_over = file_length Mod BLOCK_SIZESet rs = New ADODB.Recordset
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
rs.Open "Select Name, Picture, FileLength FROM People", m_DBConnrs.AddNew
rs!Name = person_name
rs!FileLength = file_lengthReDim bytes(BLOCK_SIZE)
For block_num = 1 To num_blocks
Get #file_num, , bytes()
rs!Picture.AppendChunk bytes()
Next block_numIf left_over > 0 Then
ReDim bytes(left_over)
Get #file_num, , bytes()
rs!Picture.AppendChunk bytes()
End Ifrs.Update
Close #file_numlstPeople.AddItem person_name
lstPeople.Text = person_name
End If
End Sub