简体   繁体   English

使用C#将数据从SQL Image字段保存到pdf,msg,xls,txt文件

[英]Save data from SQL Image field to pdf, msg, xls, txt file using C#

I have a table in DB with a field of type Image. 我在DB中有一个表,其类型为Image。 The filed of type image stores data from attachments(uploaded from aspx application) which could be pdf, xls, msg, txt, etc. The table also has a field which stores the attachment name and extension type. 类型为image的文件存储来自附件的数据(从aspx应用程序上载),数据可能是pdf,xls,msg,txt等。该表还具有一个字段,用于存储附件名称和扩展名类型。

I have to save the attachments back to my file system for all the rows in that table. 我必须将该表中所有行的附件保存回我的文件系统中。

I have tried the following but I do not have permissions to execute the below 我已经尝试了以下操作,但是我没有执行以下操作的权限

DECLARE @ObjectToken INT
EXEC sp_OACreate 'ADODB.Stream', @ObjectToken OUTPUT
EXEC sp_OASetProperty @ObjectToken, 'Type', 1
EXEC sp_OAMethod @ObjectToken, 'Open'
EXEC sp_OAMethod @ObjectToken, 'Write', NULL, Content
EXEC sp_OAMethod @ObjectToken, 'SaveToFile', NULL, 'c:/file.msg', 2
EXEC sp_OAMethod @ObjectToken, 'Close'
EXEC sp_OADestroy @ObjectToken

Can anyone please let me know how to save all the attachments to the file system using C#. 谁能让我知道如何使用C#将所有附件保存到文件系统。 I am thinking of building a Windows Form App with a Save button. 我正在考虑使用“保存”按钮构建Windows Form App。

If you just want the files with quick and dirty method just use VBS with ADO: 如果只想使用快速而肮脏的方法将文件与ADO一起使用,请使用VBS:

Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Type = 1 ' = adTypeBinary
BinaryStream.Open

Dim UUIDARRAY() 
Redim UUIDARRAY(1000)
strConnection = "Driver={SQL Server};Server=<servername>;Database=<databasename>;Uid=<username>;Pwd=<userpasswor>;"
Set cn = CreateObject("ADODB.Connection")
cn.ConnectionString = strConnection    
cn.Open
If cn.State = 1 Then
    Set objrs=cn.execute( "Select <data> FROM <table> where <something>" )
    Dim error
    While not (objrs is nothing)
        set field = objrs.Fields(0)
        chunk_data = field.GetChunk(65536)
        BinaryStream.Write chunk_data

    Set objrs = objrs.NextRecordSet
    Wend
    cn.close
    BinaryStream.SaveToFile "binarydata.bin", 2 '= adSaveCreateOverWrite'
else
    wscript.echo  "Could not connect"
end if

Just use this script and replace all the <...> with relevant information. 只需使用此脚本并将所有<...>替换为相关信息即可。

Execute this one on your SQL server (need sa rights) 在您的SQL服务器上执行此操作(需要sa权限)

sp_configure

‘show advanced options’, 1

GO

reconfigure

GO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从SQL Server获取图像数据,写入文件,使用C#将文件保存到磁盘 - How to get Image data from SQL Server, Write To File, Save File to Disk using C# 使用C#将一个.xls列存储为.txt文件 - Store one .xls column as .txt file using C# 如何将C#中的数据保存到txt文件中? - How to save data from C# in a txt file? 如何使用C#将动态添加的行中的数据保存到(csv / txt)文件到HTML表格? - How to save data to a (csv/txt) file from dynamically added rows to a HTML table using C#? 如何使用NPOI c#将xls文件保存为xlsx文件? - how to save xls file as xlsx file using NPOI c#? 从C#中的.txt文件保存变量 - Save Variables from a .txt File on C# 在 C# 中将 MSG 电子邮件转换为 PDF 文件 - Convert MSG email to PDF file in C# 如何从一个文本文件读取数据连接字符串和多个sql命令并将输出保存在c#中的output.txt中 - how to read data connection string and multiple sql commands from one text file and save output in output.txt in c# 如何以编程方式(C#)从Xls-XML(以XML格式保存的xls文件)数据导入SQL Server - How to programmatically (C#) import from the Xls-XML(xls file Saved in XML Format) data in to SQL Server C# 将 email 消息另存为 .msg 文件 - C# Save email message as .msg file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM