| 发表于:2007-04-07 17:53:563楼 得分:15 |
使用流对象保存和显示图片 打开vb6,新建工程。 添加两个按钮,一个image控件 注意:access中的photo字段类型为ole对象. sqlserver中的photo字段类型为image '** 引用 microsoft activex data objects 2.5 library 及以上版本 ‘2.5版本以下不支持stream对象 dim iconcstr as string dim iconc as adodb.connection '保存文件到数据库中 sub s_savefile() dim istm as adodb.stream dim ire as adodb.recordset dim iconcstr as string '读取文件到内容 set istm = new adodb.stream with istm .type = adtypebinary '二进制模式 .open .loadfromfile app.path + "\test.jpg " end with '打开保存文件的表 set ire = new adodb.recordset with ire .open "select * from img ", iconc, 1, 3 .addnew '新增一条记录 .fields( "photo ") = istm.read .update end with '完成后关闭对象 ire.close istm.close end sub sub s_readfile() dim istm as adodb.stream dim ire as adodb.recordset '打开表 set ire = new adodb.recordset ‘得到最新添加的纪录 ire.open "select top 1 * from img order by id desc ", iconc, adopenkeyset, adlockreadonly '保存到文件 set istm = new adodb.stream with istm .mode = admodereadwrite .type = adtypebinary .open .write ire( "photo ") ‘这里注意了,如果当前目录下存在test1.jpg,会报一个文件写入失败的错误. .savetofile app.path & "\test1.jpg " end with image1.picture = loadpicture(app.path & "\test1.jpg ") '关闭对象 ire.close istm.close end sub private sub command1_click() call s_readfile end sub private sub command2_click() call s_savefile end sub private sub form_load() '数据库连接字符串 iconcstr = "provider=microsoft.jet.oledb.4.0;persist security info=false " & _ ";data source=f:\csdn_vb\database\保存图片\access图片\img.mdb " ‘下面的语句是连接sqlserver数据库的. ‘iconcstr = "provider=sqloledb.1;persist security info=true; " & _ ‘ "user id=sa;password=;initial catalog=test;data source=yang " set iconc = new adodb.connection iconc.open iconcstr end sub private sub form_unload(cancel as integer) iconc.close set iconc = nothing end sub | | |
|