参考:
Sub 下载SQL文件()
Dim adoStm As ADODB.Stream
Dim adoRec As New ADODB.Recordset
Const SQLconnectionString = "xxx" 'SQL连接字符串
Dim sqlConn As New ADODB.Connection
sqlConn.Open SQLconnectionString '打开到SQL的连接
Dim i As Long
adoRec.Open "select * from SQL存储文件的表名称 where 条件表达式", sqlConn, adOpenKeyset, adLockReadOnly
'保存到文件
If adoRec.RecordCount > 0 Then
For i = 1 To iRe.RecordCount
Set adoStm = New ADODB.Stream
With adoStm
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write iRe("保存文件的字段名")
.SaveToFile 保存文件的路径 + "\实例.xls" '+- 这里注意了,如果当前目录下存在实例.xls,会报一个文件写入失败的错误.
End With
Next i
'关闭对象
adoRec.Close
adoStm.Close
End If
End Sub
Private Sub 文件上传到SQL()
Dim adoStm As ADODB.Stream
Dim adoRec As ADODB.Recordset
Const SQLconnectionString = "xxx" 'SQL连接字符串
Dim sqlConn As New ADODB.Connection
sqlConn.Open SQLconnectionString '打开到SQL的连接
'读取文件到adoStm
Set adoStm = New ADODB.Stream
With adoStm
.Type = adTypeBinary '二进制模式方式
.Open
.LoadFromFile CurrentProject.Path + "\实例.xls"
End With
'打开保存文件的SQL表
Set adoRec = New ADODB.Recordset
With adoRec
.Open "select * from SQL存储文件的表名称", sqlConn, 1, 3
.AddNew '新增一条记录
.Fields("保存文件的字段名") = adoStm.Read
'如果还有其他必填字段,也要给字段赋值
'... ...
.Update
End With
adoRec.Close
adoStm.Close
End Sub