SQL中图片存取与显示的问题

luo_373883269 2007-03-12 08:56:10
//这是插入数据的代码
protected void Button1_Click(object sender, EventArgs e)
{
string ExName = string.Empty;
#region
if (FileSelect.PostedFile.FileName != null)
{
int filesize = FileSelect.PostedFile.ContentLength;//get the file of size
if (filesize > (500 * 1024) || filesize < 0)
{
Response.Write("<scirpt language='javascript'>alert('请确认图片存在且小于500K');</scirpt>");//大于500K或文件不存在时
}
else
{
try
{ //获取文件完整的路径及名称
string fullname = FileSelect.PostedFile.FileName.ToString();
//取得文件扩展名
ExName = fullname.Substring(fullname.LastIndexOf("."));

//一:利用时间生成新文件名(年+月+日+小时+分钟+秒+毫秒)
string NewName = DateTime.Now.Year.ToString() +
DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();

//二:利用GUID结构生成唯一文件名
Guid myguid = Guid.NewGuid();
NewName += myguid.ToString();
NewName = NewName.Replace("-","");
NewName = NewName.ToUpper();

//为新文件加上扩展名
NewName += ExName;

//HttpRuntime.AppDomainAppPath获取站点跟目录的物理路径
FileSelect.PostedFile.SaveAs(HttpRuntime.AppDomainAppPath + @"UpLoad\" + NewName);

//插入数据
string cmdsql = "data source=.;database=TryPicture;Trusted_Connection=yes";

SqlConnection conn = new SqlConnection(cmdsql);
conn.Open();

string cmdtext = "insert into Blog_Picture(pictureSize,pictureupload,pictureName,pictureDatetime,pictureType) values (@picturesize,@pictureupload,@pictureName,@pictureDatetime,@picturetype)";

SqlCommand comm = new SqlCommand(cmdtext, conn);
comm.Parameters.Add("pictureupload", SqlDbType.NVarChar).Value = @"upload/" + NewName;
comm.Parameters.Add("pictureName", SqlDbType.VarChar).Value = txtTitle.Text.Trim();
comm.Parameters.Add("pictureDatetime", SqlDbType.DateTime).Value = DateTime.Now.ToString();
comm.Parameters.Add("pictureType",SqlDbType.Char).Value=ExName;
comm.Parameters.Add("pictureSize",SqlDbType.Int).Value=filesize;

comm.ExecuteNonQuery();

conn.Close();
Response.Write("<script language='javascript'>alert('记录添加成功');</script>");
}
catch (Exception error)
{
Response.Write(error.ToString());
}
}
}
#endregion
}

在数据库中查询图片路径的结果是这样的路径 upload/20073122095EIFJISIA987.gif



//下面是读取的代码
private DataSet Get_Picture()
{ //查询图片存取的路径
string cmdtext = "select pictureupload from Blog_Picture";

SqlConnection connget = new SqlConnection(cmdsql);
connget.Open();

SqlDataAdapter da = new SqlDataAdapter(cmdtext, connget);

DataSet ds = new DataSet();
da.Fill(ds,"picture");
return ds;
}

用image控件 怎么显示图片,根据数据集中取出的的图片路径啊?
...全文
264 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cangwu_lee 2007-03-12
  • 打赏
  • 举报
回复
WinForm
PictureBox1.Image = Image.FromFile( <FilePaht> ) ;

WebForm
如果在虚拟目录中,就
System.Web.UI.WebControls.Image.ImageUrl = "<VirPath>"

要不然,就是读取、然后 用 System.Web.HttpResponse.BinaryWrite( byte[] )

————————————————————————————————————
写作,虽然每个人都会查阅辞海,可,不是人人都能写出不朽的篇章的。编程,如是也。
最后一只恐龙 2007-03-12
  • 打赏
  • 举报
回复
假设DataRow dr = ds.Table[i].Rows[j];
Image1.ImageUrl = "定位到upload的路径" + dr["pictureupload"].ToString();

"定位到upload的路径"那一部分用相对路径到达upload那个文件夹。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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