有什麼辦法,打開任何文件(.doc,.xls,.pdf等)與相關的程序保存在SQL Server作爲varbinary?打開任何文件(.doc,.xls,.pdf等)與相關的程序保存在sql server中作爲varbinary?
在服務器中創建數據庫;
create database DEV
GO
USE DEV
GO
create Table FileWarehouse
(
Id int identity (1,1),
FileType Nvarchar (50),
StoredFile Varbinary (MAX)
)
我正在將文件存儲在數據庫中,如圖所示;
insert into FileWarehouse
select 'docx' as FileType,*
from OPENROWSET (BULK 'C:\SQLDOC\WILLBESAVEDINSERVER.docx', SINGLE_BLOB)
AS X
我可以從服務器獲取文件並保存到文件夾,如圖所示;
DECLARE
@SQLIMG VARCHAR (MAX),
@File VARBINARY (MAX),
@OutputPath VARCHAR (MAX),
@ObjectToken INT
Select @File = StoredFile FROM FileWarehouse where FileType ='docx' -- or use id
SET @OutputPath = 'C:\SQLDOC\OUTPUT\output_word_fromServer.docx'
EXEC sp_OACreate 'ADODB.Stream', @ObjectToken OUTPUT
EXEC sp_OASetProperty @ObjectToken, 'Type',1
EXEC sp_OAMethod @ObjectToken, 'Open'
EXEC sp_OAMethod @ObjectToken, 'Write', NULL, @File
EXEC sp_OAMethod @ObjectToken, 'SaveToFile', NULL, @OutputPath,2
EXEC sp_OAMethod @ObjectToken, 'Close'
EXEC sp_OADestroy @ObjectToken
GO
但我無法直接從數據庫打開文件;使用如下代碼;
private void btnOpenDocument_Click(object sender, EventArgs e)
{
int id = (int)dataGridView1.CurrentRow.Cells["ID"].Value;
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = new SqlCommand("GetDocument");
adp.SelectCommand.CommandType = CommandType.StoredProcedure;
adp.SelectCommand.Connection = ResimGaleri.ORM.Tools.Baglanti;
adp.SelectCommand.Parameters.Add("@ID", SqlDbType.Int).Value = sd.ID;
// add here extension that depends on your file type
string fileName = Path.GetRandomFileName() + ".txt";
using (SqlConnection conn = ResimGaleri.ORM.Tools.Baglanti)
{
ResimGaleri.ORM.Tools.Baglanti.Open();
using (SqlCommand cmd = adp.SelectCommand)
{
// you have to distinguish here which document, I assume that there is an `id` column
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
int size = 1024 * 1024;
byte[] buffer = new byte[size];
int readBytes = 0;
int index = 0;
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
while ((readBytes = (int)dr.GetBytes(0, index, buffer, 0, size)) > 0)
{
fs.Write(buffer, 0, readBytes);
index += readBytes;
}
}
}
}
}
}
// open your file, the proper application will be executed because of proper file extension
ResimGaleri.ORM.Tools.Baglanti.Close();
Process prc = new Process();
prc.StartInfo.FileName = fileName;
prc.Start();
}
}
這樣我就可以把病毒程序插入數據庫中的此列,比使用SQL Server執行它在數據庫服務器上。我不知道是否sql服務器甚至允許這個,但我希望不是 – GuidoG
如何插入病毒到哪個列?有趣。 –
我想會有一些客戶端,允許這個表被填充? – GuidoG