急?

lyj0775 2006-03-24 11:11:37
在asp.net(用c#写的程序)如何让图片插入到数据库、从数据库把图片调出。
...全文
94 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Knight94 2006-03-28
  • 打赏
  • 举报
回复
不过你的图片首先要上传到服务器,因此要设置Aspnet用户对图片保存目录能具有可写权限。
Knight94 2006-03-28
  • 打赏
  • 举报
回复
参看
http://blog.csdn.net/Knight94/archive/2006/03/24/637800.aspx
songxiaozhao 2006-03-28
  • 打赏
  • 举报
回复
将图片保存到数据库:
private void SubmitBtn_Click(object sender, System.EventArgs e)
{
string url=myFile.Value.ToString().Trim();
if(url==null||url=="")
{
Response.Write("<script>alert('请选择文件路径!');</script>");
return;
}
System.IO.FileInfo fi=new FileInfo(url);
string filename=fi.Name;//取文件名

string ext=fi.Extension;//取文件后缀
System.IO.Stream sr =myFile.PostedFile.InputStream;
byte[] blob;
blob=new byte[sr.Length];
sr.Read(blob,0,int.Parse(sr.Length.ToString()));
sr.Close();

//string myUrl=Server.MapPath("")+"\\upLoadFile";
//myFile.PostedFile.SaveAs(myUrl);



//文件转换为流???????????

ConStr=ConfigurationSettings.AppSettings["ConStr"];
Conn=new SqlConnection(ConStr);
try
{
Conn.Open ();
SqlCommand cmd = new SqlCommand("insert into data_image (ext,data) values('"+ext+"',@mydata) SELECT @@IDENTITY",Conn);
cmd.Parameters.Add("@mydata",blob);
//cmd.ExecuteNonQuery();
myid=int.Parse(cmd.ExecuteScalar().ToString());
Session["myid"]=myid;
}
catch (SqlException ex)
{
throw(new Exception("数据添加失败,原因:"+ex.Message));
}
finally
{
Conn.Close ();
Response.Write("<script>alert('提交成功!点击查看可以看到具体内容!');window.location.href='WebForm1.aspx';</script>");
}
}
songxiaozhao 2006-03-28
  • 打赏
  • 举报
回复
比如你要在a.aspx中显示图片
在a中
<img src="showimg.aspx?id=<%=id%>">
然后在showimg.aspx中代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace jcgy.Info
{
/// <summary>
/// showimg 的摘要说明。
/// </summary>
public class showimg : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlConnection Conn;
protected System.Data.SqlClient.SqlDataAdapter da;
protected System.Data.DataSet ds;
protected string ConStr;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string imageid=Request.QueryString["id"].Trim();
ConStr=ConfigurationSettings.AppSettings["ConStr"];
Conn=new SqlConnection(ConStr);
this.Conn.Open();
string sql="select * from newspic where id='"+imageid+"'";
da=new SqlDataAdapter(sql,Conn);
ds=new DataSet();
da.Fill(ds,"newspic");
if(ds.Tables[0].Rows.Count<=0)
{
Response.Write("");
Response.End();
return;
}
else
{
Session["pic"]=ds.Tables[0].Rows[0]["pic"];
Session["pictype"]=ds.Tables[0].Rows[0]["pictype"];
Response.Expires=0;
Response.Buffer=true;
Response.Clear();
Response.ContentType=Session["pictype"].ToString();
Response.BinaryWrite((byte[])Session["pic"]);
Session["pic"]="";
Session["pictype"]="";
}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
songxiaozhao 2006-03-24
  • 打赏
  • 举报
回复
用二进制流的形式,存入数据库,
读取的时候也是用流读取,然后显示出来
RUN_ 2006-03-24
  • 打赏
  • 举报
回复
为什么一定要写到数据库,写到文件夹,把路径写到数据库中不好吗。

111,118

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧