'将任何文件从数据库中下载到本地: Public Function LoadFile(ByVal col As ADODB.Field, ByVal FileName As String) As Boolean '获得binary数据 On Error GoTo myerr: Dim arrBytes() As Byte Dim FreeFileNumber As Integer lngsize = col.ActualSize arrBytes = col.GetChunk(lngsize) FreeFileNumber = FreeFile Open FileName For Binary Access Write As #FreeFileNumber Put #FreeFileNumber, , arrBytes Close #FreeFileNumber LoadFile = True myerr: If Err.Number <> 0 Then LoadFile = False Err.Clear End If End Function
'将文件从本地上传到数据库中 Public Function UpLoadFile(ByVal FileName, ByVal col As ADODB.Field) As Boolean On Error GoTo myerr: Dim arrBytes() As Byte Dim FreeFileNumber As Integer FreeFileNumber = FreeFile Open FileName For Binary As #FreeFileNumber n = LOF(FreeFileNumber) ReDim arrBytes(1 To n) As Byte Get #FreeFileNumber, , arrBytes Close #FreeFileNumber col.AppendChunk (arrBytes) UpLoadFile = True myerr: If Err.Number <> 0 Then UpLoadFile = False Err.Clear End If End Function
With the methods and properties of a Stream object, you can do the following: Open a Stream object from a Record or URL with the Open method. Close a Stream with the Close method. Input bytes or text to a Stream with the Write and WriteText methods. Read bytes from the Stream with the Read and ReadText methods. Write any Stream data still in the ADO buffer to the underlying object with the Flush method. Copy the contents of a Stream to another Stream with the CopyTo method. Control how lines are read from the source file with the SkipLine method and the LineSeparator property. Determine the end of stream position with the EOS property and SetEOS method. Save and restore data in files with the SaveToFile and LoadFromFile methods. Specify the character set used for storing the Stream with the Charset property. Halt an asynchronous Stream operation with the Cancel method. Determine the number of bytes in a Stream with the Size property. Control the current position within a Stream with the Position property. Determine the type of data in a Stream with the Type property. Determine the current state of the Stream (closed, open, or executing) with the State property. Specify the access mode for the Stream with the Mode property.
数据库字段:wjmc 文件名,wjsx 文件的扩展名。Wjnr 文件的内容为二进制。(若access数据库为“ole对象”,sql server为“image”)
该程序可以操作所有的文件类型。
Dim Wenjian As String
Dim RD As Byte
Dim SIZE As Long
Const MYSIZE = 1048576
Dim WENJIANN() As Byte
Dim Rs As New ADODB.Recordset
Rs.Open "select * from wj", Cn, 1, 3
Rs.AddNew
Rs!wjmc = Mid(Name, 1, InStr(Name, ".") - 1)
Rs!wjsx = Mid(Name, InStr(Name, ".") + 1)
‘name为文件的名称加扩展名
Open Filename For Binary Access Read As #1
SIZE = LOF(1)
Do While SIZE - MYSIZE >= 0
ReDim WENJIANN(MYSIZE) As Byte
Get #1, , WENJIANN
Rs!wjnr.AppendChunk WENJIANN
SIZE = SIZE - MYSIZE
Loop
If SIZE > 0 Then
ReDim WENJIANN(SIZE) As Byte
Get #1, , WENJIANN
Rs!wjnr.AppendChunk WENJIANN
End If
Close #1
Rs.Update
Set Rs = Nothing
完整的代码如下:如果是用SQL那么保存文件的字段类型应该是Binary。如果是用Access,那么保存文件的字段应该用ole对象,在表中显示为长二进制数据。
'将任何文件从数据库中下载到本地:
Public Function LoadFile(ByVal col As ADODB.Field, ByVal FileName As String) As Boolean '获得binary数据
On Error GoTo myerr:
Dim arrBytes() As Byte
Dim FreeFileNumber As Integer
lngsize = col.ActualSize
arrBytes = col.GetChunk(lngsize)
FreeFileNumber = FreeFile
Open FileName For Binary Access Write As #FreeFileNumber
Put #FreeFileNumber, , arrBytes
Close #FreeFileNumber
LoadFile = True
myerr:
If Err.Number <> 0 Then
LoadFile = False
Err.Clear
End If
End Function
'将文件从本地上传到数据库中
Public Function UpLoadFile(ByVal FileName, ByVal col As ADODB.Field) As Boolean
On Error GoTo myerr:
Dim arrBytes() As Byte
Dim FreeFileNumber As Integer
FreeFileNumber = FreeFile
Open FileName For Binary As #FreeFileNumber
n = LOF(FreeFileNumber)
ReDim arrBytes(1 To n) As Byte
Get #FreeFileNumber, , arrBytes
Close #FreeFileNumber
col.AppendChunk (arrBytes)
UpLoadFile = True
myerr:
If Err.Number <> 0 Then
UpLoadFile = False
Err.Clear
End If
End Function
我用VB中OLE容器控件嵌入Word文件,然后编辑,编辑完成后,我要如何保存在OLE容器中修改的Word的文件?
Data控件不能连接Access 2000数据库。
Close a Stream with the Close method.
Input bytes or text to a Stream with the Write and WriteText methods.
Read bytes from the Stream with the Read and ReadText methods.
Write any Stream data still in the ADO buffer to the underlying object with the Flush method.
Copy the contents of a Stream to another Stream with the CopyTo method.
Control how lines are read from the source file with the SkipLine method and the LineSeparator property.
Determine the end of stream position with the EOS property and SetEOS method.
Save and restore data in files with the SaveToFile and LoadFromFile methods.
Specify the character set used for storing the Stream with the Charset property.
Halt an asynchronous Stream operation with the Cancel method.
Determine the number of bytes in a Stream with the Size property.
Control the current position within a Stream with the Position property.
Determine the type of data in a Stream with the Type property.
Determine the current state of the Stream (closed, open, or executing) with the State property.
Specify the access mode for the Stream with the Mode property.
你可以下载微软的mdac_typ或VB6的SP5
我看了你的帖子,做了一个程序,连接的就是Access 2000数据库
随编辑,WORD文件随保存