我做了一个上传图片功能,把图片上传到了一个文件夹里,我又用DataGrid显示信息,然后用DataGrid的删除功能,但我只能删除图片在数据库中的路径字段,我要怎么才能在DataGrid中删除文件夹里的图片拉?
请给出详细代码!谢谢各位!我是用的是ACCESS数据库!ASP。NET(C#)
数据库News_Main 存放图片字段为 Tpic 主键为NewsID 自动增长!
谢谢大家的帮助,我在线等待各位!
private string conString;
    protected void Page_Load(object sender, EventArgs e)
    {
        conString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source =" + Server.MapPath("News.mdb");
        if (!this.IsPostBack)
        {
            this.dgBind();//DataGrid绑定数据
        }
        if (Session["admin"] == null)
        {
            Response.Redirect("Admin_Nosession.aspx");
        }
      
    }
    protected void dgBind()
    {
        try
        {
            string ID = Request.QueryString["ID"];
            OleDbConnection con = new OleDbConnection(conString);
            OleDbDataAdapter da = new OleDbDataAdapter();
            da.SelectCommand = new OleDbCommand("select * from News_Main where MenuID ="+ID+" order by Addtime desc", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "inf");
            dg.DataKeyField = "NewsID";
            dg.DataSource = ds.Tables["inf"].DefaultView;
            dg.DataBind();
            con.Close();
        
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void dg_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ((LinkButton)e.Item.Cells[8].Controls[0]).Attributes.Add("onClick", "return confirm('是否删除')");
        }
    }
    protected void dg_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
        try
        {
           
            string NewsID = this.dg.DataKeys[e.Item.ItemIndex].ToString();
            OleDbConnection con = new OleDbConnection(conString);
            con.Open();
            OleDbCommand cmd = new OleDbCommand("delete * from News_Main where NewsID=" + NewsID, con);
            cmd.ExecuteNonQuery();
            this.dgBind();
            con.Close();
      ( 我要在这里删除文件夹下的图片,我怎么删除啊!请补充代码!谢谢!还有就是,在DataGrid中删除时,删除最后一条时,要报错,我怎么办啊?还有,每次删除的时候,“是否删除”提示后,还要在点一下删除,意思就是要删除两次才能删除,为什么拉?请好心人帮我把代码补充我完,谢谢!)
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }    

解决方案 »

  1.   

    File.Delete(Server.MapPath("主目录相对路径" + "/" + "文件名")
      

  2.   

    还是没有搞懂!
      请把详细代码写出来!
    谢谢!我的工程名叫NewsNet 图片文件夹叫file
    麻烦各位了!
      

  3.   


        protected void dg_DeleteCommand(object source, DataGridCommandEventArgs e) 
        { 
            try 
            { 
                
                string NewsID = this.dg.DataKeys[e.Item.ItemIndex].ToString(); 
                OleDbConnection con = new OleDbConnection(conString); 
                con.Open(); 
                OleDbCommand cmd = new OleDbCommand("delete * from News_Main where NewsID=" + NewsID, con); 
                cmd.ExecuteNonQuery(); 
                this.dgBind(); 
                con.Close(); 
                DelPic(NewsID);
          
                 } 
            catch (Exception ex) 
            { 
                Response.Write(ex.Message); 
            } 
        }  private void DelPic(string NewsID)
    {
    //假设你数据库里面的图片字段为pic,具体你自己改
    OleDbConnection con = new OleDbConnection(conString); 
    OleDbDataAdapter da = new OleDbDataAdapter(); 
    da.SelectCommand = new OleDbCommand("select pic from News_Main where  NewsID="+NewsID , con); 
    DataSet ds = new DataSet(); 
    da.Fill(ds, "inf");
    if (ds.Tables["inf"].Rows.Count>0)
    {
    string pic=ds.Tables["inf"].Rows[0]["pic"].ToString();
    if (System.IO.File.Exists(Server.MapPath("~/file/"+pic)))
    {
    System.IO.File.Delete(Server.MapPath("~/file/"+pic));
    }
    }
    }
      

  4.   

    DataGrid删除最后一条记录报错,请到百度上去找答案:
    http://www.baidu.com/s?wd=DataGrid%C9%BE%B3%FD%D7%EE%BA%F3%D2%BB%CC%F5&cl=3
      

  5.   

    至于为什么会删除两次才会起作用,其实是 dgBind() 函里面的问题(删除的话其实点一次就删除掉了,你不信点一次删除然后看下数据库里面的记录).
    问题是你每次删除记录以后再绑定dg,上次的数据还没清除掉(datagrid里面的数据不会自动清空的),这个也是你为什么删除最后一条记录会报错的原因,因为最后一次删除的记录是不存的.protected void dgBind() 
        { 
            try 
            { 
                DataTable da=new DataTable();
                da.Rows.Clear();            
                dg.DataSource = da; 
                dg.DataBind();             string ID = Request.QueryString["ID"]; 
                OleDbConnection con = new OleDbConnection(conString); 
                OleDbDataAdapter da = new OleDbDataAdapter(); 
                da.SelectCommand = new OleDbCommand("select * from News_Main where MenuID ="+ID+" order by Addtime desc", con); 
                DataSet ds = new DataSet(); 
                da.Fill(ds, "inf"); 
                dg.DataKeyField = "NewsID"; 
                dg.DataSource = ds.Tables["inf"].DefaultView; 
                dg.DataBind(); 
                con.Close(); 
             
            } 
            catch (Exception ex) 
            { 
                Response.Write(ex.Message); 
            } 
        }