当前位置:文档之家› vb数据库连接字符串

vb数据库连接字符串

***************************************************************************** 使用adodb.stream 保存/读取文件到数据库** 引用microsoft activex data objects 2.5 library 及以上版本**** ----- 数据库连接字符串模板---------------------------------------** access数据库** iconcstr = "provider=microsoft.jet.oledb.4.0;persist security info=false" & _** ";data source=数据库名"**** sql数据库** iconcstr = "provider=sqloledb.1;persist security info=true;" & _** "user id=用户名;password=密码;initial catalog=数据库名;data source=sql服务器名"***************************************************************************保存文件到数据库中sub s_savefile()dim istm as adodb.streamdim ire as adodb.recordsetdim iconcstr as string数据库连接字符串iconcstr = "provider=microsoft.jet.oledb.4.0;persist security info=false" & _";data source=f:/my documents/客户资料1.mdb"读取文件到内容set istm = new adodb.streamwith istm.type = adtypebinary 二进制模式.open.loadfromfile "c:/test.doc"end with打开保存文件的表set ire = new adodb.recordsetwith ire.open "表", iconc, adopenkeyset, adlockoptimistic.addnew 新增一条记录.fields("保存文件内容的字段") = istm.read.updateend with完成后关闭对象ire.closeistm.closeend sub从数据库中读取数据,保存成文件sub s_readfile()dim istm as adodb.streamdim ire as adodb.recordsetdim iconc as string数据库连接字符串iconc = "provider=microsoft.jet.oledb.4.0;persist security info=false" & _";data source=//xz/c$/inetpub/zj/zj/zj.mdb"打开表set ire = new adodb.recordsetire.open "tb_img", iconc, adopenkeyset, adlockreadonlyire.filter = "id=64"保存到文件set istm = new adodb.streamwith istm.mode = admodereadwrite.type = adtypebinary.open.write ire("img").savetofile "c:/test.doc"end with关闭对象ire.closeistm.closeend subdim cn as new adodb.connectiondim rs as new adodb.recordsetdim stm as adodb.streamprivate sub savepicturetodb(cn as adodb.connection)将bmp图片存入数据库on error goto ehset stm = new adodb.streamrs.open "select imagepath,imagevalue from tbl_image", cn, adope nkeyset, adlockoptimisticcommondialog1.showopentext1.text = commondialog1.filenamewith stm.type = adtypebinary.open.loadfromfile commondialog1.filenameend withwith rs.addnew.fields("imagepath") = text1.text.fields("imagevalue") = stm.read.updateend withrs.closeset rs = nothingexit subeh: msgbox err.description, vbinformation, "error"end subprivate sub loadpicturefromdb(cn as adodb.connection)载数据库中读出bmp图片on error goto ehdim strtemp as stringset stm = new adodb.streamstrtemp = "c:/temp.tmp" 临时文件,用来保存读出的图片rs.open "select imagepath,imagevalue from tbl_image", cn, , , adcmdtextwith stm.type = adtypebinary.open.write rs("imagevalue").savetofile strtemp, adsavecreateoverwrite.closeend withimage1.picture = loadpicture(strtemp)set stm = nothingrs.closeset rs = nothingexit subeh: msgbox err.description, vbinformation, "error"end subimage类型用picture显示以下两个函数是从数据库中读出图片的核心程序public function getimage(optional filename as string) as varianton error goto procerrdim objrs as adodb.recordsetdim strsql as stringdim chunk() as byteset objrs = new adodb.recordsetstrsql = "select thumb from tblpictures where idpict=" & tblid(thu mbindex) & ""strsql = "select thumb from tblpictures where idpict= " & thum bstrsql = "select thumb from tblpictures where idpict=387"db.execute strsqlobjrs.open strsql, db, adopenforwardonly, adlockreadonlyif objrs.bof and objrs.eof thengetimage = 0goto procexitelseif isnull(objrs.fields(0)) thenerrnumber = 1001errdesc = "字段为空"goto procexitend ifchunk() = objrs.fields(0).getchunk(objrs.fields(0).actualsize)set getimage = chunk2image(chunk(), filename)procexit:on error resume nextobjrs.closechunk() = objrs.fields(0).getchunk(0)set getimage = chunk2image(chunk(), filename)set objrs = nothingexit functionprocerr:getimage = 0resume procexitend functionprivate function chunk2image(chunk() as byte, optional filename as st ring) as varianton error goto procerrdim keepfile as booleandim datafile as integerkeepfile = trueif trim(filename) = "" thenfilename = "c:/tmpxxdb.fil"keepfile = falseend ifdatafile = freefileopen filename for binary access write as datafileput datafile, , chunk()close datafileprocexit:set chunk2image = loadpicture(filename)on error resume nextif not keepfile then kill filenameexit functionprocerr:on error resume nextkill filenamechunk2image = 0end functionpublic function getfromfile(strtable as string, strfield as string, strfilter as string, objfilename as string) as boolean============================================================过程函数名:commmodule.getfromfile类型:function参数:strtable (string):准备保存图形数据的表名称strfield (string):准备保存图形数据的字段名称strfilter (string):打开表的过滤字符串,用于定位并确保被打开的表的数据的唯一性objfilename (string) :准备输入到表里边的图象文件名称返回:如果保存成功,返回true,如果失败,返回false-------------------------------------------------------------说明:把图象文件的数据保存到表里边-------------------------------------------------------------修订历史:=============================================================dim recset as adodb.recordset, filedata() as byte, fileno as lon g, filesize as long, strsql as stringstrsql = "select " & strfield & " from " & strtable & " where " & strfilter & ";"set recset = new adodb.recordsetrecset.open strsql, currentproject.connection, adopendynamic, adlockopti misticgetfromfile = trueif recset(strfield).type <> db_ole or not isfilename(objfilename) thengetfromfile = false如果字段不是ole字段,或者文件不存在,返回错误goto endgetfromfileend ifif recset.eof then如果记录不存在,返回错误getfromfile = falsegoto endgetfromfileend iffilesize = getfilesize(objfilename) 如果被打开的文件大小为零,返回错误if filesize <= 0 thengetfromfile = falsegoto endgetfromfileend ifredim filedata(filesize)重新初始化数组fileno = freefile获取一个空闲的文件号open objfilename for binary as #fileno打开文件get #fileno, , filedata()读取文件内容到数组close #fileno关闭文件recset(strfield).value = filedata() 保存数据recset.update更新数据erase filedata释放内存endgetfromfile:recset.close关闭recordsetset recset = nothing释放内存end functionpublic function savetofile(strtable as string, strfield as string, strfilter as string, strfilename as string) as boolean============================================================过程函数名:commmodule.savetofile 类型:function参数:strtable (string):保存图形数据的表名称strfield (string):保存图形数据的字段名称strfilter (string):打开表的过滤字符串,用于定位并确保被打开的表的纪录的唯一性strfilename (string) :准备保存的图象的文件名称返回:如果保存成功,返回true,如果失败,返回false-------------------------------------------------------------说明:把由getfromfile函数保存到表中ole字段的数据还原到文件-------------------------------------------------------------修订历史:=============================================================dim recset as adodb.recordset, filedata() as byte, fileno as lon g, filesize as long, strsql as stringstrsql = "select " & strfield & " from " & strtable & " where " & strfilter & ";"set recset = new adodb.recordsetrecset.open strsql, currentproject.connection, adopendynamic, adlockopti misticsavetofile = trueif recset(strfield).type <> db_ole thensavetofile = false如果字段不是ole字段,返回错误goto endsavetofileend ifif recset.eof then如果记录不存在,返回错误savetofile = falsegoto endsavetofileend iffileno = freefileopen strfilename for binary as #filenoredim filedata(recset(strfield).actualsize) 重新初始化数组filedata() = recset(strfield).getchunk(recset(strfield).actualsize) 把ole字段的内容保存到数组put #fileno, , filedata()把数组内容保存到文件close #filenoerase filedataendsavetofile:recset.closeset recset = nothingend function。

相关主题