永发信息网

.NET中如何将存入数据库中的二进制文件读取出来

答案:2  悬赏:80  手机版
解决时间 2021-06-05 15:48
我把一个压缩文件用二进制写入SQLserver数据库,但是要怎么把二进制数据读取出来,转化成压缩文件,求解
最佳答案
、建所需数据库和表,语句如下:

--建立数据库
create database test

--使用该数据库
use test

--建立存放图片的表
create table piclist(
id int Identity primary key,
pic Image not null
)

2、制作上传图片的模块,代码如下:
前台html代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpPhoto.aspx.cs" Inherits="Test_UpPhoto" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=" http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="UpPhoto" name="UpPhoto" runat="server" type="file" />
<asp:Button id="btnAdd" runat="server" Text="上传" OnClick="btnAdd_Click"></asp:Button>
</div>
</form>
</body>
</html>

后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;

public partial class Test_UpPhoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnAdd_Click(object sender, EventArgs e)
{
//获得图象并把图象转换为byte[]
HttpPostedFile upPhoto = UpPhoto.PostedFile;
int upPhotoLength = upPhoto.ContentLength;
byte[] PhotoArray = new Byte[upPhotoLength];
Stream PhotoStream = upPhoto.InputStream;
PhotoStream.Read(PhotoArray, 0, upPhotoLength);

//连接数据库
string ConStr = "server=(local);user id=sa;pwd=sa;database=test";
SqlConnection conn = new SqlConnection(ConStr);

string strSql = "Insert into piclist(pic) values(@pic)";
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.Parameters.Add("@pic", SqlDbType.Image);
cmd.Parameters["@pic"].Value = PhotoArray;

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("图片上传成功");
}
}

3、制作显示图片的模块(单独显示图片,即没用到datalist):
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class Test_ShowPhoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//连接数据库
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
string strSql = "select * from piclist";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();

SqlCommand cmd=new SqlCommand(strSql,conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["pic"]);
Response.Write("successful");
}
reader.Close();
conn.Close();
Response.End();
}
}
}

补充步骤3,用datalist显示图片方法:
建立两个asp.net 页面,名称为piclist.aspx和StreamImg.aspx。
piclist.aspx前台代码为:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="piclist.aspx.cs" Inherits="Test_Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns=" http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dlContent" runat="server" Width="554px">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td >
<img id='img1' src='StreamImg.aspx?id= <%# DataBinder.Eval(Container.DataItem,"id") %>'>
</a>
</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>

piclist.aspx后台代码为:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class Test_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//连接数据库
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
SqlConnection sqlcon = new SqlConnection(ConnStr);
sqlcon.Open();
string sqlstr = "select id from piclist";
SqlDataAdapter MyAdapter = new SqlDataAdapter(sqlstr, sqlcon);
DataSet ds = new DataSet();
MyAdapter.Fill(ds, "tb_pic");
this.dlContent.DataSource = ds;
this.dlContent.DataBind();
sqlcon.Close();
}
}
}

StreamImg.aspx无前台代码,后台代码为:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;


public partial class StreamImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string type = Request.QueryString["pt"];
int id = Convert.ToInt32(Request.QueryString["id"]);
ShowPic(id);
}
private void ShowPic(int id)
{
//连接数据库
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
string strSql = "select * from piclist where id='"+ id +"'";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();

SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["pic"]);
Response.Write("successful");
}
reader.Close();
conn.Close();
Response.End();
}
} 9

回答者: pgdoryoku - 见习主管 五级

全部回答
  • oracle
  • mysql
  • access
  • sybase
  • 数据库 zebea
  • 数据库 kingunid
  • 数据库 ms-sql
  • 数据库 conn.asp
  • 我要举报
    如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
    大家都在看
    请大连的朋友们推荐下比较好的寒假英语辅导班
    我怎么才可以让爱我人不爱我呢
    迷失山洞中第几关有铁矿石
    武穴市黄冈武穴市人民法院梅川人民法庭地址在
    cM是怎么算的·?请帮忙·谢谢
    我国月薪两千万的人有吗?
    从电信拉宽带网和直接买上网笔记本哪个更合算
    这段感情结束了吗?
    解梦我梦见我叔叔家女儿掉进湖里
    怎么下载DNF视频?
    微星主板785GTM-E45配什么显卡最合适,还有它
    梦幻号忘了密码,安全码等所有信息,该怎么找回
    怎样才能让一个意志不坚定(决定的事也容易放
    极品飞车13在哪儿下?
    在长途车上是否应该给老人让座(该老人是超载
    推荐资讯
    街机摩托车游戏
    什么时候国庆和中秋同一天啊
    什么时间吃萍果比较好
    请帮忙解梦。我梦见女友坐飞机走,我去追她,
    为什么DNF用了乐乐v小T版【千剑回巢】老掉线
    为什么不能进入好友农场?
    机械加浮空弹+6,膝撞加+6够了吗?
    祁阳县永州喜羊羊才艺幼儿园这个地址怎么能查
    非常曲线咖啡有用吗?
    对肺部有好处的食物都有哪些?
    邓州市南阳双汇冷鲜肉怎么去啊,谁知道地址啊
    DNF驱魔pk刷图快捷键里分别放什么技能。
    正方形一边上任一点到这个正方形两条对角线的
    阴历怎么看 ?