我已经在我的SQL Server 2008数据库中存储了一个excel文件(作为VARBINARY(MAX))以下(到目前为止,硬编码)方式:
// 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”的大小 - 但文件没有打开,这就是我的问题。
请帮帮我!
编辑: 这是我目前用来“打开”电子表格的代码:
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();
答案 0 :(得分:1)
您发布的代码看起来可能会将电子表格写入光盘,但我看不到任何代码可以打开它。你需要使用像
这样的东西System.Diagnostics.Process.Start(@"c:\" + DocumentName)
我想。