通用 文件保存至數據庫,從數據庫寫入磁盤 程序代碼
2024-07-21 02:23:27
供稿:網友
通用 文件保存至數據庫,從數據庫寫入磁盤 程序代碼 ----20040809
這幾天我休假中,正好有時間繼續編寫mycodelibrary 1.5版,今天晚上剛好寫到文件與數據庫存入取出模塊,在論壇上此問題見的也較多,所以特此公開此部分代碼,供有需者參考使用.代碼雖然可以完整的正常使用,但還是需要做些錯誤方面的處理。
'歡迎你下載使用本代碼,本份代碼由程序太平洋提供下載學習之用
'聲明:
'1.本站所有代碼的版權歸原作者所有,如果你使用了在本站下載的源代碼
' 引起的一切糾紛(后果)與本站無關,請您尊重原作者的勞動成果!
'2.若本站在代碼上有侵權之處請您與站長聯系,站長會及時更正。
'中國代碼網:http://www.daima.com.cn
'程序太平洋:http://www.5ivb.net
'email:[email protected]
'copyright 2001-2005 by www.5ivb.net
'整理時間:2004-8-9 3:32:48
option explicit
public objconn as new adodb.connection
public m_connstring as string
private function exists(byval str_filename as string, _
byval int_val as vbfileattribute) as boolean
'--------------------------------------------------------------------------------
' project : mycodelibrary 1.5
' procedure : exists
' description: [判斷文件或目錄是否存在]
' created by : ronggang ([email protected])
' date-time : 2004-8-9-2:31:45
'
' parameters : str_filename (string)
' int_val (vbfileattribute)
'--------------------------------------------------------------------------------
on error resume next
if len(str_filename) = 0 then
exists = false
exit function
end if
if int_val <> vbdirectory then '如果不是目錄
'如果為空表示文件不存在
if dir(str_filename) = "" then
exists = false
else
exists = true
end if
else
if dir(str_filename, vbdirectory) = "" then
exists = false
else
exists = true
end if
end if
end function
public sub binvalue(byval strfilename as string, byref objfield as field)
'--------------------------------------------------------------------------------
' project : mycodelibrary 1.5
' procedure : binvalue
' description: [將文件保存至數據庫中]
' created by : wangfeng
' date-time : 2004-8-9-2:20:37
'
' parameters : strfilename (string)
' objfield (field)
'--------------------------------------------------------------------------------
'此方法需要做錯誤處理,以防文件己打開
dim objstream as stream
if not exists(strfilename, vbnormal) then '如果文件不存則拋出異常
err.raise 50001, "dbfile", "文件不存在!"
exit sub
end if
set objstream = new adodb.stream
with objstream
.type = adtypebinary
.open
.loadfromfile strfilename
objfield.value = .read
end with
set objstream = nothing
end sub
public function binvalue2file(byval strfilename as string, byref objfield as field, optional overwrite as boolean = false) as boolean
'--------------------------------------------------------------------------------
' project : mycodelibrary 1.5
' procedure : binvalue2file
' description: [將數據庫中的二進制數據保存為文件]
' created by : wangfeng
' date-time : 2004-8-9-2:22:33
'
' parameters : strfilename (string) 目標文件
' objfield (field) 數據字段名
' overwrite (boolean = false) 是否覆蓋現有存在的文件
' true 覆蓋 false(默認)不存在時保存
'--------------------------------------------------------------------------------
on error goto errorhander
dim objstream as stream
dim returnmsg as vbmsgboxresult
set objstream = new adodb.stream
with objstream
.type = adtypebinary
.open
.write objfield.value
if overwrite then
.savetofile strfilename, adsavecreateoverwrite
else
.savetofile strfilename, adsavecreatenotexist
end if
end with
binvalue2file = true '保存成功返回true
101:
set objstream = nothing
exit function
errorhander:
binvalue2file = false
goto 101
end function
public function getfilename(byval strpathfilename) as string
dim ipos as long
ipos = vba.instrrev(strpathfilename, "/")
getfilename = mid(strpathfilename, ipos + 1)
end function
public function getpathname(optional strpathname as string) as string
'sfilename = mid(getpathname, ipos + 1)
dim ipos as long
ipos = vba.instrrev(strpathname, "/")
getpathname = mid(strpathname, 1, ipos)
end function