正在阅读:在SQL Server中保存和输出图片在SQL Server中保存和输出图片

2004-04-01 13:25 出处:PConline 作者:cashcho翻译/中国软件 责任编辑:pjl
从数据库中输出图片   现在让我们从数据库中取出我们刚刚保存的图片,在这儿,我们将直接将图片输出至浏览器。你也可以将它保存为一个文件或做任何你想做的。

private void Page_Load(object sender, System.EventArgs e)

{

string imgid =Request.QueryString["imgid"];

string connstr=((NameValueCollection)

Context.GetConfig("appSettings"))["connstr"];

string sql="SELECT imgdata, imgtype FROM ImageStore WHERE id = "

+ imgid;

SqlConnection connection = new SqlConnection(connstr);

SqlCommand command = new SqlCommand(sql, connection);

connection.Open();

SqlDataReader dr = command.ExecuteReader();

if(dr.Read())

{

Response.ContentType = dr["imgtype"].ToString();

Response.BinaryWrite( (byte[]) dr["imgdata"] );

}

connection.Close();

}

  在上面的代码中我们使用了一个已经打开的数据库,通过datareader选择images。接着用Response.BinaryWrite代替Response.Write来显示image文件。   希望您喜欢这些文章,如有任何意见和建议请致信webmaster@bipinjoshi.com。 本文转载自中国软件(http://www.csdn.net)。
键盘也能翻页,试试“← →”键

相关文章

关注我们

最新资讯离线随时看 聊天吐槽赢奖品