这是后台代码
 private SqlConnection myconn = new SqlConnection(ConfigurationManager.AppSettings["strCon"]);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
        
        }
    }
    public void getImg()
    {
        string strSql = "select * from works";
        SqlDataAdapter mysda = new SqlDataAdapter(strSql,myconn);
        DataSet myds = new DataSet();
        mysda.Fill(myds,"works");
        this.img1.ImgUrl = myds.Tables["Works"].Rows[0].ItemArray[2].ToString();
    }
这是前台代码
<li><a href="Default.aspx" target="_blank" ><asp:Image width="168" height="103" alt="我的婚礼我的爱"  runat="server" id="img1" /></a>
                        <b><a href="Default.aspx"  target="_blank">我的婚礼我的爱</a></b>
                    </li>

解决方案 »

  1.   

    用FileUpload上传
    获得到:string imgName = this.FileUploadPhoto.FileName.ToString();
                if (imgName == null || imgName.Equals(""))
                {
                    Response.Write("<script>alert('请上传产品图片');</script>");
                }else
            {
                    this.FileUploadPhoto.SaveAs(Server.MapPath("~/Admin/fileImage/" + imgName));
                    string dbasePath = imgName; //"~\\Admin\\fileImage\\" + imgName;
                    cp.cImage = dbasePath;//图片路径
                调用插入方法
                            }
     页面imgae:   ImageUrl='<%# getImage((Eval("cImage")).ToString()) %>'
       public string getImage(string image)
        {
            return "Admin/fileImage/" + image;
        }
    不知道能解决你的问题么
      

  2.   

    楼主这样写不行?那应该是路径的问题吧,可以这样保存路径:/项目名称/图片文件夹/xx.jpg
      

  3.   

    把从数据库读取到的地址赋值给一个字符串,看下图片路径是否正确,在赋值给ImgUrl 
      

  4.   

    恩 首先用控件把图片上传到项目中 数据库中存的是图片的路径,img文件夹中是图片,然后imgurl得到路径 就可以显示了
      

  5.   

     this.img1.ImgUrl = myds.Tables["Works"].Rows[0].ItemArray[2].ToString();
    这行的img1为啥不会自动显示出来?
      

  6.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = FileUpload1.FileName;//上传文件名字
            string size = FileUpload1.PostedFile.ContentLength.ToString();
            string type = FileUpload1.PostedFile.ContentType;
            string type2 = name.Substring(name.LastIndexOf(".") + 1);
            string ipath = Server.MapPath("upimg") + "\\" + name;
            string fpath = Server.MapPath("upfile") + "\\" + name;
            string path="F:\\aaa\\"+FileUpload1.FileName;
            string wpath = "upimg\\" + name;
            if (type2 == "jpg" || type2 == "gif" || type2 == "bmp" || type2 == "png")
            {
                FileUpload1.SaveAs("F:\\aaa\\"+FileUpload1.FileName);
               // Image1.ImageUrl="F:\\aaa\\"+FileUpload1.FileName;
                Label1.Text = "你传图片的名字是" + name + "<br>文件大小为" + size + "<br>文件类型为" + type2 + "<br>文件路径为" + ipath;
            }
            
            SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
            SqlCommand cmd = new SqlCommand("insert into Image(imageName,imagepath) values('" + name + "','" + path + "')", cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {    SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
        SqlCommand cmd = new SqlCommand("select imageName from Image where imageID='" + Convert.ToInt32(TextBox1.Text) + "'", cn);
        cn.Open();
        string a = cmd.ExecuteScalar().ToString();
        cn.Close();
        Image1.ImageUrl = "F:\\aaa\\" + a;
         }
    }