'这是SQL SERVER中的,其实不同数据库之间的代码基本都是一致的,只是字段的类型有所不同!!Option ExplicitDim cn As New ADODB.Connection, rs As New ADODB.Recordset'保存 Private Sub Command1_Click() Dim bteContent() As Byte
Open "C:\aa.bmp" For Binary Access Read As #1 bteContent = InputB(LOF(1), #1) Close #1
If rs.State = adStateOpen Then rs.Close rs.Open "select * from tablename", cn, adOpenDynamic, adLockPessimistic rs.AddNew rs!Name = "张三" rs!AGE = 22 rs!SEX = "男" rs.Fields("PHOTO").AppendChunk bteContent rs.Update
Erase bteContent End Sub'打开 Private Sub Command2_Click() Dim bteContent() As Byte If rs.State = adStateOpen Then rs.Close rs.Open "select * from tablename", cn, adOpenForwardOnly, adLockReadOnly bteContent = rs.Fields("PHOTO").GetChunk(rs.Fields("PHOTO").ActualSize) Open "C:\aa.bmp" For Binary Access Write As #1 Put #1, , bteContent Close #1 End SubPrivate Sub Form_Load() On Error GoTo Errhandle cn.ConnectionString = "Driver={SQL Server};SERVER=DataServer;DATABASE=zxzx;UID=information;PWD=information*&#" cn.Open
Exit Sub Errhandle: MsgBox Err.Description, vbExclamation End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) On Error Resume Next rs.Close Set rs = Nothing cn.Close Set cn = Nothing End Sub
Private Sub Command1_Click()
Dim bteContent() As Byte
Open "C:\aa.bmp" For Binary Access Read As #1
bteContent = InputB(LOF(1), #1)
Close #1
If rs.State = adStateOpen Then rs.Close
rs.Open "select * from tablename", cn, adOpenDynamic, adLockPessimistic
rs.AddNew
rs!Name = "张三"
rs!AGE = 22
rs!SEX = "男"
rs.Fields("PHOTO").AppendChunk bteContent
rs.Update
Erase bteContent
End Sub'打开
Private Sub Command2_Click()
Dim bteContent() As Byte
If rs.State = adStateOpen Then rs.Close
rs.Open "select * from tablename", cn, adOpenForwardOnly, adLockReadOnly
bteContent = rs.Fields("PHOTO").GetChunk(rs.Fields("PHOTO").ActualSize) Open "C:\aa.bmp" For Binary Access Write As #1
Put #1, , bteContent
Close #1
End SubPrivate Sub Form_Load()
On Error GoTo Errhandle
cn.ConnectionString = "Driver={SQL Server};SERVER=DataServer;DATABASE=zxzx;UID=information;PWD=information*&#"
cn.Open
Exit Sub
Errhandle:
MsgBox Err.Description, vbExclamation
End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
On Error Resume Next
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
哪位可以帮帮我啊!!顶上去。
Set stm = New ADODB.Stream
Set rs1 = New ADODB.Recordset
rs1.Open "select * from ywtc_main where file_name='" + File_name + "'", db, adOpenKeyset, adLockOptimistic
With stm
.Type = adTypeBinary
.Open
.LoadFromFile "c:\aa\aa.bmp"
End With
With rs1
If .EOF Then
.AddNew
.Fields("id") = txtfields(0).Text
.Fields("file_name") = File_name
.Fields("file_bt") = txtfields(2).Text
.Fields("jysm") = txtfields(3).Text
.Fields("wj") = stm.Read
.Fields("gh") = gh
.Fields("file_lb") = Trim(Combo1.Text)
.Fields("op_time") = Format(Date, "yyyy-mm-dd")
.Update
Else
.Fields("file_name") = File_name
.Fields("file_bt") = txtfields(2).Text
.Fields("jysm") = txtfields(3).Text
.Fields("wj") = stm.Read
.Update
End If
End With
rs1.Close
Set rs1 = Nothing
从数据库中下载文件
Set stm = New ADODB.Stream
id = Left(Trim(L(Index).Caption), 4) 'strTemp = "c:\temp.bmp"
Set rs1 = New ADODB.Recordset
rs1.Open "select * from ywtc_main where id='" + Trim(id) + "' ", db, , , adCmdText
If Not rs1.EOF Then
axP1.Text = "正在下载文件=>'" + Trim(rs1.Fields("File_bt")) + "' ,请稍候........!"
axP1.Visible = True
axP1.Caption = ""
File_name = rs1.Fields("file_name")
With stm
.Type = adTypeBinary
.Open
.Write rs1("wj").Value
strTemp = App.Path & "\" & rs1.Fields("file_name")
If IsNull(Dir(App.Path & "\" & rs1.Fields("file_name"))) Then
Else
' Kill (App.Path & "\" & rs1.Fields("file_name"))
End If
.SaveToFile strTemp, adSaveCreateOverWrite
.Close
End With
' rs1.MoveNext
End If
打开文件(图片)我就不说了,本人正在用。