sqlserver-2008--file上传下载

数据库:sql server 2008 express

表:

CREATE TABLE [dbo].[filetest](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [fs] [varbinary](max) NULL
) ON [PRIMARY]

 

写入:

 protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection sconn =new SqlConnection("Server=192.168.1.11\\sqlexpress,1433; Database=DBCustomer; User Id=sa; Password=1234");
            string FileName;
            string FilePath = this.FileUpload1.PostedFile.FileName;
            Stream FileStream = FileUpload1.PostedFile.InputStream;
            FileName = Path.GetFileName(FilePath);
            if (FileName != null && FileName != "")
            {
                int FileLen = FileUpload1.PostedFile.ContentLength;
                byte[] FileData = new Byte[FileLen];
                int n = FileStream.Read(FileData, 0, FileLen);
                FileStream.Close();
                SqlCommand com = new SqlCommand();
                com.CommandText = "insert into filetest(fs) values(@fs)";
                com.Parameters.Add(new System.Data.SqlClient.SqlParameter("@fs", System.Data.SqlDbType.Image, FileData.Length, "fs"));
                com.Connection = sconn;
                com.Parameters["@fs"].Value = FileData;
                com.Connection.Open();
                com.ExecuteNonQuery();
                com.Connection.Close();
            }
        }

读取

SqlConnection sconn =new SqlConnection("Server=192.168.1.11\\sqlexpress,1433; Database=DBCustomer; User Id=sa; Password=1234");

//根据不同文件类型,设定ContentType 。
                //Response.ContentType = "application/vnd.ms-excel";
                //Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.ContentType = "application/pdf";
                this.Response.Clear();
                SqlCommand selcom = new SqlCommand();
                selcom.CommandText = "select fs from filetest order by id desc";
                selcom.Connection = sconn;
                selcom.Connection.Open();
                SqlDataReader dr = selcom.ExecuteReader();
                dr.Read();
                Byte[] b = new Byte[(dr.GetBytes(0, 0, null, 0, int.MaxValue))];
                dr.GetBytes(0, 0, b, 0, b.Length);
                dr.Close();
                selcom.Connection.Close();
                System.IO.Stream fs = this.Response.OutputStream;
                fs.Write(b, 0, b.Length);

                fs.Close();
                this.Response.End();

posted on 2012-03-08 16:53 青蛙學堂 阅读(631) 评论(1)  编辑 收藏 引用 所属分类: 軟件布袋數據庫

评论

# re: sqlserver-2008--file上传下载 2012-03-08 18:34 download




if (drupdow.Read())
{
Response.ContentType = "application/x-msdownload ";
Response.AddHeader( "Content-Disposition ", "attachment; filename= " + HttpUtility.UrlEncode(drupdow.GetString(1).Trim()) + " "+ drupdow[ "expandname "].ToString().Trim() + " ");
Response.BinaryWrite((byte[])drupdow[ "side "]);
Response.Flush();
Response.End();
RegisterStartupScript( "alert ", " <script language= 'javascript '> alert( '附件成功下载! ') </script> ");
}
  回复  更多评论   

只有注册用户登录后才能发表评论。
<2012年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(8)

随笔分类

随笔档案

收藏夹

青蛙学堂

最新评论

阅读排行榜

评论排行榜