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

标签 excel vba

我不知道如何通过 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/

相关文章:

python - 选择要从 Excel 读入 pandas 数据框的行号

r - 无法将 R 数据框追加到现有 Excel 中而不覆盖

excel - Excel 2016 中的 Workbook_Open() 未触发

VBA - 将列值转换为负值

VBA 代码中的 Excel 公式

mysql - Excel-Vba 连接 Mysql localhost 失败

Excel VBA - 从网站获取数据

python - 如何使用 Python 将粗体样式应用于 Excel 文件中的特定单词?

r - 列表成矩阵

vba - 将一系列单元格复制到通过单元格命名的多个工作表中