我把图片存放在数据库(sql 2k)中,然后读出来在硬盘上先生成一个图片文件,用Image控件显示,一切正常、但当我编辑(换成别的图片)后再保存到数据中后,再读出来,还是用Image控件显示,这时显示的是旧图片,而且数据库中正存的是新图片,硬盘上也是新图片文件,但Image控件显示就是旧图片,大家有没碰到过这样的问题啊?各位大虾帮帮忙啊……

解决方案 »

  1.   

    看看你得Image綁定圖片有沒有問題
      

  2.   

    Image控件没有删除原来的图片吧?调试跟踪以下就明白!
      

  3.   

    to  lampson123(微软):  
        这两种方式我都用,根据不同的需求,我用不同的方法写!
        这是老板的意思,我也没有办法啊!to dragonfly001(我思考,我生存!) 
       我的图片绑定没有问题,我在它属性中取它的路径,然后在IE中打开
       一切正常,然后我再回我那页面,这时显示就正常了,就是说我如果
       没有在IE中打开过的,显示总是不对的,一打开后,那边也就正常了
        
      

  4.   

    to JasonHeung(拥有一切也不过就这样笑着哭):
       我一开始就注意到这一点,所以我用了以下代码:
    private void Image_Source_Load(object sender, System.EventArgs e)
    {
       if ((string)ViewState["FileName"] != string.Empty && 
          ViewState["FileName"] != null )
          File.Delete((string)ViewState["FileName"]);
    }
      

  5.   

    下面是代码:private void DataGrid1_ItemCommand(object source, 
            System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
       string FSQL, FileName, AreaFileName ;
       if (e.CommandName.Equals("Edit"))
       {
          TableCell IDCell = e.Item.Cells[0] ;
          ViewState["PicID"] = IDCell.Text.Trim() ;      SqlConnection sqlConn = new SqlConnection() ;
          sqlConn.ConnectionString = (string)ViewState["ItemConnTxt"] ;
          sqlConn.Open() ;
          FSQL = "select * from t_mdlpic where id = "+IDCell.Text.Trim() ;
          SqlDataAdapter SQLGetMDLPic = new SqlDataAdapter(FSQL,sqlConn) ;
          DataSet DSGetMDLPic = new DataSet() ;
          SQLGetMDLPic.Fill(DSGetMDLPic,"t_mdlpic") ;
          if (DSGetMDLPic.Tables[0].Rows.Count >0)
          {
             TBPicNo.Text = DSGetMDLPic.Tables[0].Rows[0]["picno"].ToString().Trim() ;
     TBFileName.Text = DSGetMDLPic.Tables[0].Rows[0]["picname"].ToString().Trim() ;
             if(DSGetMDLPic.Tables[0].Rows[0]["picture"] is byte[])
     {
        byte[] Imageby=(byte[])DSGetMDLPic.Tables[0].Rows[0]["picture"];
        MemoryStream MSPic =new MemoryStream(Imageby) ;
        AreaFileName =DSGetMDLPic.Tables[0].Rows[0]["code"].ToString().Trim()+
                              DSGetMDLPic.Tables[0].Rows[0]["modelid"].ToString().Trim()+
      DSGetMDLPic.Tables[0].Rows[0]["picno"].ToString().Trim()+
                              "."+DSGetMDLPic.Tables[0].Rows[0]["pictype"].ToString().Trim() ;
        FileName = MapPath("").Trim()+"/ItemModelPic/"+AreaFileName.Trim() ;
        FileStream FSPic = new FileStream(FileName,FileMode.Create);
        FSPic.Read(Imageby,0,Convert.ToInt32(MSPic.Length));
        FSPic.Write(Imageby,0,Convert.ToInt32(MSPic.Length));
        FSPic.Close();
        Image_Source.ImageUrl = "ModelPic/"+AreaFileName.Trim() ;
        ViewState["FileName"] = FileName ; 
             }
          }
       }
    }
      

  6.   

    下面是代码:private void DataGrid1_ItemCommand(object source, 
            System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
       string FSQL, FileName, AreaFileName ;
       if (e.CommandName.Equals("Edit"))
       {
          TableCell IDCell = e.Item.Cells[0] ;
     
          SqlConnection sqlConn = new SqlConnection() ;
          sqlConn.ConnectionString = (string)ViewState["ItemConnTxt"] ;
          sqlConn.Open() ;      FSQL = "select * from t_mdlpic where id = "+IDCell.Text.Trim() ;
          SqlDataAdapter SQLGetMDLPic = new SqlDataAdapter(FSQL,sqlConn) ;
          DataSet DSGetMDLPic = new DataSet() ;
          SQLGetMDLPic.Fill(DSGetMDLPic,"t_mdlpic") ;
          if (DSGetMDLPic.Tables[0].Rows.Count >0)
          {
             TBPicNo.Text = DSGetMDLPic.Tables[0].Rows[0]["picno"].ToString().Trim() ;
    TBFileName.Text = DSGetMDLPic.Tables[0].Rows[0]["picname"].ToString().Trim() ;
             if(DSGetMDLPic.Tables[0].Rows[0]["picture"] is byte[])
     {
        byte[] Imageby=(byte[])DSGetMDLPic.Tables[0].Rows[0]["picture"];
        MemoryStream MSPic =new MemoryStream(Imageby) ;
        AreaFileName =DSGetMDLPic.Tables[0].Rows[0]["code"].ToString().Trim()+
                              DSGetMDLPic.Tables[0].Rows[0]["modelid"].ToString().Trim()+
            DSGetMDLPic.Tables[0].Rows[0]["picno"].ToString().Trim()+
                              "."+DSGetMDLPic.Tables[0].Rows[0]["pictype"].ToString().Trim() ;
        FileName = MapPath("").Trim()+"/ModelPic/"+AreaFileName.Trim() ;
        FileStream FSPic = new FileStream(FileName,FileMode.Create);
        FSPic.Read(Imageby,0,Convert.ToInt32(MSPic.Length));
        FSPic.Write(Imageby,0,Convert.ToInt32(MSPic.Length));
        FSPic.Close();
        Image_Source.ImageUrl = "ModelPic/"+AreaFileName.Trim() ;
        ViewState["FileName"] = FileName ; 
             }
          }
       }
    }
      

  7.   

    可以动态生成Image 控件再绑定
      

  8.   

    每次都从DB读取应该不会显示旧的图片.
    你是在DataGrid的ItemDataBind中给img添加URL的吗?
      

  9.   

    to cjzlxy(享受孤独):
       没有啊!
      

  10.   

    to ybbigepl() : 我也试了,还是没有用!
    to lampson123(微软): 动态我怕会影响速度啊
      

  11.   

    其实你这样的性能不是很好的,我用惯另外一种输出:
    在Page里放入:
    <asp:Image ID="imgLogo" ImageUrl = "img.aspx"; Runat="server" ></asp:Image>
    然后定义img.aspx页面的输出:
    Response.Clear();
    Response.ContentType="image/gif";
    Response.AppendHeader("Content-Disposition","filename=Logo"+System.DateTime.Now.Ticks.ToString()+".jpg");  
    //下面是你读取的数据库流的输出
    Response.BinaryWrite(logo);
    Response.End();愚见!!
      

  12.   

    to  kanbozhang(kanbo.z.): 谢谢!高招!结贴!