日期:2014-05-19  浏览次数:20377 次

ASP.NET + Sql Server图片存储
怎么插入图片到数据中,然后再显示出来?   数据库中要求存储的是图片的路径,不是二进制。

------解决方案--------------------
使用 Image 控件在网页上显示图像。
通过设置 ImageUrl 属性来指定所显示图像的路径。
如果图像不可用,可以通过设置 AlternateText 属性来指定为取代图像而显示的文本。
------解决方案--------------------
代码看这里:
http://blog.csdn.net/Ivy_zheng/archive/2007/03/23/1538170.aspx
------解决方案--------------------
if (myFile.PostedFile.ContentLength != 0)
{
if (myFile.PostedFile != null)
{
string nam = myFile.PostedFile.FileName;
//Response.Write(nam);
//取得文件名(抱括路径)里最后一个 ". "的索引
int i = nam.LastIndexOf( ". ");
//取得文件扩展名
newext = nam.Substring(i);
//这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
DateTime now = DateTime.Now;
newname = now.DayOfYear.ToString() + myFile.PostedFile.ContentLength.ToString();

//保存文件到你所要的目录,这里是IIS根目录下的uploadfiles目录
//注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里 "\ "必须用 "\\ "代替
myFile.PostedFile.SaveAs(Request.PhysicalApplicationPath + "images\\ftb\\updata\\ " + newname + newext);
//myFile.PostedFile.SaveAs( "D:\\kmsite\\images\\ftb\\updata\\ " + newname + newext);
//myFile.PostedFile.SaveAs(Server.MapPath( ".\\UpLoadFiles\\ " + newname + newext));
}
------解决方案--------------------
DBOp dbop = new DBOp(Request.PhysicalApplicationPath);
int a = dbop.OpenDataBase();
if (a == 1)
{
OleDbDataAdapter da = new OleDbDataAdapter( "select * from Main_pricute ", dbop.DataBaseConn);
DataSet ds = new DataSet();
da.Fill(ds);
tablestr = " <table width=75% border=0 cellspacing=1 cellpadding=1> <tr> ";
for(int i =1;i <=ds.Tables[0].Rows.Count;i++)
{
if (ds.Tables[0].Rows[i - 1] != null)
{
//if (i <= 5)
//{
tablestr += " <td> <img src= '../../ " + ds.Tables[0].Rows[i - 1].ItemArray[7] + " ' width=100 height=100 border=0/> <br> " + ds.Tables[0].Rows[i - 1].ItemArray[1] + " </td> ";

//}

//if (i > 5 && i < ds.Tables[0].Rows.Count + 1)
//{
// tablestr += " <td> <a href=Main.aspx?id= " + Session[ "number "] + "> <img src= ' " + ds.Tables[0].Rows[i - 1].ItemArray[7] + " ' width=100 height=100 border=0/> </a> ";
//}
if (i % 6 == 0)
{
tablestr += " </tr> <tr> ";
}
}
}
tablestr += " </tr> <tr> <a href= List.aspx > 返回图片列表 </a> </tr> </table> ";

}

this.Literal1.Text = tablestr;
}