1
我已經在我的SQL Server 2008數據庫中存儲了一個excel文件(作爲VARBINARY(MAX))以下(到目前爲止,硬編碼)的方式:如何通過C#在SQL Server 2008中打開一個存儲的Excel文件
// C# - visual studio 2008
var update = new SqlCommand("UPDATE Requests SET Attachment = @xls" +
" WHERE RequestsID = 27", conn);
update.Parameters.AddWithValue("xls", File.ReadAllBytes("C:/aFolder/hello.xlsx"));
update.ExecuteNonQuery();
它的工作,但我也想打開它!
我該怎麼做? 請注意,不是讀取blob-data,而是打開實際的「hello.xlsx」文件。
我曾嘗試以下: http://dotnetsoldier.blogspot.com/2007/07/how-to-retrieve-blob-object-in-winforms.html 我可以看到它的工作原理,如「Binary.Length」是在執行時,我的「hello.xlsx」的確切規模 - 但doesn't打開該文件,並that'是我的問題。
請幫我一把!
編輯: 這裏是代碼,我目前使用的「打開」電子表格:
SqlConnection conn =
new SqlConnection
(global::MY_PROJECT.Properties.Settings.Default.DB_1ConnectionString);
conn.Open();
SqlCommand Cmd = new SqlCommand("select Attachment from Requests where RequestsID = 27", conn);
Cmd.CommandType = CommandType.Text;
SqlDataReader Reader = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
//
string DocumentName = null;
FileStream FStream = null;
BinaryWriter BWriter = null;
//
//
//
byte[] Binary = null;
const int ChunkSize = 100;
int SizeToWrite = 0;
MemoryStream MStream = null;
//
while (Reader.Read())
{
DocumentName = Reader["Attachment"].ToString();
// Create a file to hold the output.
FStream = new FileStream(@"c:\" + DocumentName, FileMode.OpenOrCreate, FileAccess.Write);
BWriter = new BinaryWriter(FStream);
Binary = (Reader["Attachment"]) as byte[];
SizeToWrite = ChunkSize;
MStream = new MemoryStream(Binary);
//
for (int i = 0; i < Binary.GetUpperBound(0) - 1; i = i + ChunkSize)
{
if (i + ChunkSize >= Binary.Length) SizeToWrite = Binary.Length - i;
byte[] Chunk = new byte[SizeToWrite];
MStream.Read(Chunk, 0, SizeToWrite);
BWriter.Write(Chunk);
BWriter.Flush();
}
BWriter.Close();
FStream.Close();
}
FStream.Dispose();
conn.Close();
請發佈您用於打開電子表格的代碼。 – Lazarus 2011-02-11 11:56:30
好的 - 完成了! – 2011-02-11 12:29:53