欢迎来到天天文库
浏览记录
ID:14329968
大小:216.00 KB
页数:6页
时间:2018-07-28
《vb+sql数据库图片存取》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、VB+SQL图片存取vb代码VB窗体界面如下:1.把图片写入到Field中函数代码:PrivateSubpicsavetodb(ByReffldAsADODB.Field,diskfileAsString)Constblocksize=4096Dimbytedata()AsByteDimnumblocksAsLongDimfilelengthAsLongDimleftoverAsLongDimsourcefileAsLongDimiAsLongsourcefile=FreeFileOpenTrim(CommonDialog1.filename)ForBina
2、ryAccessReadAssourcefilefilelength=LOF(sourcefile)Iffilelength=0ThenClosesourcefileMsgBoxTrim(CommonDialog1.filename)&"无内容或不存在!"Elsenumblocks=filelengthblocksizeleftover=filelengthModblocksizefld.Value=NullReDimbytedata(blocksize)Fori=1TonumblocksGetsourcefile,,bytedata()fld.Appen
3、dChunkbytedata()NextReDimbytedata(leftover)Getsourcefile,,bytedata()fld.AppendChunkbytedata()ClosesourcefileEndIfEndSub2.把图片存入SQL数据库XS(学生表)中:PrivateSubCommand1_Click()DimsAsStringDimbytedata()AsByteDimadofldAsADODB.Fields="select*fromxswhere学号='"&Trim(Text1.Text)&"'"sqlres.Opens,co
4、nnectstring,adOpenDynamic,adLockPessimisticIfNotsqlres.EOFThenSetadofld=sqlres.Fields("照片")IfCommonDialog1.filename<>""ThenCallpicsavetodb(adofld,"filename")EndIfsqlres.Update3.把图片从SQL数据库读入VB图片框:PrivateSubCommand3_Click()DimsqlresAsADODB.RecordsetSetsqlres=NewADODB.Recordsetsqlres.
5、Open"select*fromxswhere学号='"&Trim(Text1.Text)&"'",connectstring,adOpenDynamic,adLockPessimisticDimstreamAsADODB.streamSetstream=NewADODB.streamstream.Mode=adModeReadWrite'Image1.Picture=LoadPicture("")IfNotIsNull(sqlres.Fields("照片"))Thenstream.Type=adTypeBinarystream.Openstream.Wri
6、tesqlres.Fields("照片")stream.SaveToFile"filename",adSaveCreateOverWriteImage1.Picture=LoadPicture("filename")sqlres.Closestream.CloseEndIfEndSub4修改图片:PrivateSubCommand2_Click()DimfilenameAsStringCommonDialog1.Filter="图像(*.jpg)
7、*.jpg
8、位图(*.bmp)
9、*.bmp"CommonDialog1.InitDir=App.Path&"图
10、片库"CommonDialog1.ShowOpenfilename=CommonDialog1.filenameImage1.Picture=LoadPicture(filename)EndSub
此文档下载收益归作者所有