给你个例子,你看一下:
private void dgimgshow_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
if(e.CommandName=="del")
{
if(dgimgshow.Items.Count==1)
{
if(dgimgshow.CurrentPageIndex!=0)
dgimgshow.CurrentPageIndex=dgimgshow.CurrentPageIndex-1;
}
// string strconn=ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn=new SqlConnection(Application["conStr"].ToString());
conn.Open();
//先删除具体文件中的内容
string strdel="select Photo_path from Photo where Photo_id="+e.Item.Cells[0].Text+"";
SqlCommand cmddel=new SqlCommand(strdel,conn);
SqlDataReader dr=cmddel.ExecuteReader();
// 取得文件名
if(dr.Read())
{
filepath+=dr["Photo_path"].ToString();
}
dr.Close();
//得到服务器上的物理路径
string filewebpath = Server.MapPath("./")+filepath;
// 创建对象
objFI = new System.IO.FileInfo(filewebpath);
string strsqldel="delete from Photo where Photo_id="+e.Item.Cells[0].Text+"";
SqlCommand cmd=new SqlCommand(strsqldel,conn);
try
{
objFI.Delete();
cmd.ExecuteNonQuery();
}
catch
{
Response.Write("有错误了!");
}
conn.Close();
Binddata();
}
}
catch
{
Response.Write("有错误");
}
}
====================
不懂了再说。