《VB+SQL数据库图片存取.docx》由会员分享,可在线阅读,更多相关《VB+SQL数据库图片存取.docx(3页珍藏版)》请在三一办公上搜索。
1、VB+SQL数据库图片存取VB+SQL图片存取vb代码 VB窗体界面如下: 1.把图片写入到Field中函数代码: Private Sub picsavetodb(ByRef fld As ADODB.Field, diskfile As String) Const blocksize = 4096 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 Ope
2、n Trim(CommonDialog1.filename) For Binary Access Read As sourcefile filelength = LOF(sourcefile) If filelength = 0 Then Close sourcefile MsgBox Trim(CommonDialog1.filename) & 无内容或不存在! Else numblocks = filelength blocksize leftover = filelength Mod blocksize fld.Value = Null ReDim bytedata(blocksize)
3、 For i = 1 To numblocks Get sourcefile, , bytedata fld.AppendChunk bytedata Next ReDim bytedata(leftover) Get sourcefile, , bytedata fld.AppendChunk bytedata Close sourcefile End If End Sub 2.把图片存入SQL数据库XS中: Private Sub Command1_Click Dim s As String Dim bytedata As Byte Dim adofld As ADODB.Field s
4、= select * from xs where 学号= & Trim(Text1.Text) & sqlres.Open s, connectstring, adOpenDynamic, adLockPessimistic If Not sqlres.EOF Then Set adofld = sqlres.Fields(照片) If CommonDialog1.filename Then Call picsavetodb(adofld, filename) End If sqlres.Update 3.把图片从SQL数据库读入VB图片框: Private Sub Command3_Clic
5、k Dim sqlres As ADODB.Recordset Set sqlres = New ADODB.Recordset sqlres.Open select * from xs where 学号= & Trim(Text1.Text) & , connectstring, adOpenDynamic, adLockPessimistic Dim stream As ADODB.stream Set stream = New ADODB.stream stream.Mode = adModeReadWrite Image1.Picture = LoadPicture() If Not
6、IsNull(sqlres.Fields(照片) Then stream.Type = adTypeBinary stream.Open stream.Write sqlres.Fields(照片) stream.SaveToFile filename, adSaveCreateOverWrite Image1.Picture = LoadPicture(filename) sqlres.Close stream.Close End If End Sub 4修改图片: Private Sub Command2_Click Dim filename As String CommonDialog1.Filter = 图像(*.jpg)|*.jpg|位图(*.bmp)|*.bmp CommonDialog1.InitDir = App.Path & 图片库 CommonDialog1.ShowOpen filename = CommonDialog1.filename Image1.Picture = LoadPicture(filename) End Sub