怎样实现图片的赋值!谢谢!U在线等……!万分火急!谢谢

lichen720 2003-12-21 10:32:39
我在编一个把图片加入到数据库的程序(用的是VB),我的实现方式是
dim rst as New ADODB.Recordset
假设存储这各图片的字段是imImg
dim stm as New Adodb.stream
rst("imImg")=stm.read
rst.Update
实现了修改功能。
现在我定义了一个Image类型的strimg的数据类型,想用Update语句来实现,怎样实现赋值。
strimg=stm.read
Update 表名 set imImg=????????
??????????中的内容应该怎样写
...全文
15 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Thanks4Help 2004-03-05
up
回复
华芸智森 2004-01-21
下面的函数是我数据库类中的函数(SQL SERVER 2000),用于存取二进制字段,适用于任何ADO版本.
说明一下:
1.存取时该记录一定要先存在.例如,你保存是,选何存其它记录,最后再调用FileToRecode函数.
2.存取必须要使用连接方式打开数据.下面用到一个打开记录集的函数:RsOpen,一同附上给你.


'将二进制文件添加到数据库中(该记录必须在存在)
'函数名:FileToRecode
'参数: M_Conn ADODB连接,TabName 目标数据表,FldName 目标字段,WhereStr 更新条件,FileName 源文件名
'返回值:
'例: CALL FileToRecode(P_Cnn,"AchGoods","GdsPhoto","Where gdsid='001'","C:\Tmp.Bmp")

Public Function FileToRecode(ByRef M_Conn As ADODB.Connection, _
TabName As String, _
FldName As String, _
WhereStr As String, _
Filename As String) As Boolean

Dim RsB As New ADODB.Recordset
Dim Person_name As String
Dim StrSql As String

Dim File_Num As String
Dim File_Length As String
Dim Bytes() As Byte
Dim Num_Blocks As Long
Dim Left_Over As Long
Dim Block_Num As Long
DIM Block_Size AS LONG

On Error Resume Next

BLOCK_SIZE=5*1024 '5K
File_Num = FreeFile
Filename = Trim$(Filename)

If Len(Dir$(Filename)) = 0 Or Len(Filename) = 0 Then FileToRecode = False: Exit Function

Open Filename For Binary Access Read As #File_Num
File_Length = LOF(File_Num) '取文件大小
If File_Length > 0 Then
Num_Blocks = File_Length / Block_Size
Left_Over = File_Length Mod Block_Size

If Left$(Trim$(UCase$(WhereStr)), Len("where")) <> UCase$("where") Then WhereStr = "Where " & Trim$(WhereStr)
StrSql = "Select Top 1 [" & TabName & "].[" & FldName & "] From [" & TabName & "] " & WhereStr
Set RsB = RsOpen(M_Conn, StrSql, False) '连接式记录集
If Not (RsB.EOF And RsB.BOF) Then

'/ '不分块写
'/ ReDim Bytes(File_Length)
'/ Get #File_Num, , Bytes()
'/ DoEvents
'/ RsB.Fields(FldName).AppendChunk Bytes()

'/分块写
ReDim Bytes(Block_Size)
For Block_Num = 1 To Num_Blocks
Get #File_Num, , Bytes()
RsB.Fields(FldName).AppendChunk Bytes()
Next

If Left_Over > 0 Then
ReDim Bytes(Left_Over)
Get #File_Num, , Bytes()
RsB.Fields(FldName).AppendChunk Bytes()
End If
RsB.Update
DoEvents
End If
RsB.Close
Set RsB = Nothing
End If
Close #File_Num
Erase Bytes
FileToRecode = (Err.Number = 0)
Err.Clear
End Function

'
'将二进制数据从记录中取出
'函数名:RecodeToFile
'参数: M_Conn ADODB连接,TabName 源数据表,FldName 源字段名, WhereStr 取字段条件,FileType 生成临时文件的类型
'返回值:'一个临时文件名
'例: GetTmpFile=RecodeToFile(P_Conn,"achgoods","achphoto","where gdsid='001',"bmp")

Public Function RecodeToFile(ByRef M_Conn As ADODB.Connection, _
TabName As String, _
FldName As String, _
WhereStr As String, _
Optional FileType As String = "Bmp") As String

Dim Rs As New ADODB.Recordset
Dim StrSql As String

Dim Bytes() As Byte
Dim File_Name As String
Dim File_Num As Integer
Dim File_Length As Long
Dim Num_Blocks As Long
Dim Left_Over As Long
Dim Block_Num As Long
Dim WorkPath As String
Dim TmpDir As New SmSysCls
DIM Block_Size AS LONG

BLOCK_SIZE=5*1024 '5K

On Error Resume Next

WorkPath = TmpDir.GetFolder(SmWinTempDirectory)
If Dir$(WorkPath, vbDirectory) = "" Then WorkPath = App.Path
If Right$(WorkPath, 1) <> "\" Then WorkPath = WorkPath & "\"

If Left$(Trim$(UCase$(WhereStr)), Len("where")) <> UCase$("where") Then WhereStr = "Where " & Trim$(WhereStr)
StrSql = "Select Top 1 [" & TabName & "].[" & FldName & "] From [" & TabName & "] " & WhereStr
Set Rs = RsOpen(M_Conn, StrSql)
If Rs.BOF And Rs.EOF Then Exit Function
If Not IsNull(Rs.Fields(FldName)) Then
File_Name = WorkPath & "TmpFile." & FileType
If Len(Dir(File_Name)) <> 0 Then Kill File_Name
File_Num = FreeFile
Open File_Name For Binary As #File_Num
File_Length = CT.ToLng(Rs.Fields(FldName).ActualSize) '取字段的实际大小
'/不分块读写
'/ If File_Length > 0 Then
'/ Bytes() = Rs.Fields(FldName).GetChunk(File_Length)
'/ Put #File_Num, , Bytes()
'/ Else
'/ Err = -1
'/ End If
'/分块读写
Num_Blocks = File_Length \ Block_Size
Left_Over = File_Length Mod Block_Size
For Block_Num = 1 To Num_Blocks
Bytes() = Rs.Fields(FldName).GetChunk(Block_Size)
Put #File_Num, , Bytes()
Next

If Left_Over > 0 Then
Bytes() = Rs.Fields(FldName).GetChunk(Left_Over)
Put #File_Num, , Bytes()
End If
Erase Bytes
Close #File_Num
Rs.Close: Erase Bytes
End If
RecodeToFile = IIf(Err.Number = 0, File_Name, "")
Set TmpDir = Nothing
Err.Clear
End Function


'
'打开一个记录集
'函数名:RsOpen
'参数: DbCnn ADODB连接,StrSql SQL查询语句,SetNothing 非连接方式(TRUE默认).连接方式(FALSE)
'返回值:记录集
'例: RsOpen P_CNN,"SELECT ACHGOODS.* FROM ACHGOODS WHERE GDSID='001'
Function RsOpen(ByRef DbCnn As ADODB.Connection, _
StrSql As String, _
Optional SetConnect As Boolean = True) As ADODB.Recordset

Dim Rs As New ADODB.Recordset

On Error Resume Next

If SetConnect Then '使用非连接
Rs.CursorLocation = adUseClient '使用客户端游标
Rs.LockType = adLockBatchOptimistic '开放式批更新
Rs.CursorType = adOpenKeyset '键集游标
Else '使用连接(主要用于更新二进制字段)
Rs.CursorLocation = adUseClient
Rs.CursorType = adOpenKeyset
Rs.LockType = adLockOptimistic '记录锁定
End If

Rs.Open StrSql, DbCnn '执行SQL
If SetConnect Then Set Rs.ActiveConnection = Nothing '设置非连接

If Err.Number = 0 Then
Set RsOpen = Rs.Clone
Else
Set RsOpen = Nothing
End If

Rs.Close
Set Rs = Nothing
End Function
回复
lichen720 2004-01-20
为什么不能呢?
回复
busisoft 2004-01-15
这样不行,不能这么操作
回复
lichen720 2003-12-21
这个和我的方法一样的,我是想用UPDATE语句
Update 表名 set imImg=????????
回复
landongfang 2003-12-21
同样的操作先使用检索语句,
dim rst as New ADODB.Recordset
Dim conn As New ADODB.Connection

Dim strSQL As String
strSQL = "select field1 from table1 where condition=1"
连接数据库
Dim stm As New ADODB.Stream
rst.Open strSQL , conn, adOpenDynamic, adLockBatchOptimistic
rst("imImg")=stm.read
rst.Update






回复
lichen720 2003-12-21
你们怎么没有人来帮助呀,快要急死人了呀,救救我呀:)
谢谢你们!
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7451

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告