gpt4 book ai didi

excel - 如何使用vba从excel中从数据库(varbinary max)中检索pdf文件

转载 作者:行者123 更新时间:2023-12-04 21:00:41 28 4
gpt4 key购买 nike

我不知道如何通过 excel vba 从数据库中检索或提取 pdf 文件? (在 c#、asp 中有很多帮助...)

存储在 SQL Server 数据库中的 varbinary max 类型字段中的文件。

我可以通过 VBA 连接或访问记录集,然后如何从该记录集中提取它

最佳答案

Dim cn  As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
Dim oStream As ADODB.Stream

Set cn = New ADODB.Connection

'Here I use default admin user 'sa' and password is blank
cn.Open "Provider = sqloledb;" & _
"Data Source=ServerNameOrIP;" & _
"Initial Catalog=DBName;" & _
"User ID=sa;" & _
"Password=;"""

'your sql statment including varbinary max field here it is FILEDATA
sql = " select EMAILID,EMAILFROM,EMAILTO,EMAILSUBJECT,FILEDATA from Tbl "


Set rs = New ADODB.Recordset

rs.Open sql, cn

Do Until rs.EOF
Set oStream = New ADODB.Stream
With oStream
.Type = adTypeBinary
.Open
.Write rs.Fields(4).Value
'Here I use 1st field value as file name i.e. rs.fiedls(0).value
'In addition you can join drive and/or folder path to save another location
.SaveToFile rs.Fields(0).Value & ".pdf", adSaveCreateOverWrite
.Close
End With
Set oStream = Nothing
rs.MoveNext
Loop


rs.Close
cn.Close

关于excel - 如何使用vba从excel中从数据库(varbinary max)中检索pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37187107/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com