[英]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.