如何将文件插入到文件stream数据types

我正在创build一个应用程序,使用SQL Server 2008提供的文件stream数据types将Excel文件存储到数据库,现在我坚持在Internet上search最佳实践方式,使用C#存储过程插入它。

到目前为止,我已经创build了数据库结构和类,我现在需要做的是实际使用存储过程,我卡住了,下面是代码片段

OpenFileDialog ofd = new OpenFileDialog(); ofd.ShowDialog(); if (ofd.CheckFileExists) { .... } using (SqlConnection conn = new SqlConnection(Murel.Util.DBUtil.CONSTRING)) { try { conn.Open(); using (SqlCommand cmd = new SqlCommand("items_insert", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@name", "test")); cmd.Parameters.Add(new SqlParameter("@template", HELP)); Guid id = (Guid)cmd.ExecuteScalar(); return true; } } catch (Exception ex) { throw ex; } finally { conn.Close(); } } 

数据表由一个唯一标识符的idvarbinary(max)名称和模板组成,我已经有了其他的东西,我只需要知道要放什么

 cmd.Parameters.Add(new SqlParameter("@template", HELP)); 

谢谢,

大stream士

尝试这个,这应该工作

 byte[] fileContent = new byte[ofd.PostedFile.ContentLength]; ofd.PostedFile.InputStream.Read(fileContent, 0,ofd.PostedFile.ContentLength); command.Parameters.Add("@FileContent", SqlDbType.VarBinary, -1).Value = fileContent; 

ofd是你的FileUpload控件