protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString); //创建连接对象
conn.Open();
if (Request.Form["chkid"] != null)
{
string sqldel = "select * from links where id in (" + Request.Form["chkid"] + ") order by id desc";
OleDbCommand cmddel = new OleDbCommand(sqldel, conn); //执行查询语句
OleDbDataReader sdr = cmddel.ExecuteReader();
if (sdr.Read())
{
foreach (int i in Request.Form["chkid"].Length)
{
FileInfo file = new FileInfo(Server.MapPath("~/" + sdr["pic"].ToString()));
file.Delete();
}
string sql = "Delete from links where id in (" + Request.Form["chkid"] + ")";
OleDbCommand cmd = new OleDbCommand(sql, conn); //执行删除语句
cmd.ExecuteNonQuery();
cmd.Dispose();
Response.Write("<script>alert('系统提示:选中记录已删除成功!');window.location.href='Manage_links.aspx';</script>");
}
else
{
Response.Write("<script>alert('系统提示:数据库暂无记录!');window.location.href='Manage_links.aspx;</script>");
}
cmddel.Dispose();
sdr.Close();
}
else
{
Response.Write("<script>alert('系统提示:您还未选中想要删除的记录!');window.history.go(-1);</script>");
}
conn.Close();
}注,这个问题是一个DataList里checkbox全选,传递ID,然后想批量删除数据库记录及上传的图片,重点是删除图片,现在只能一张一张的删除,而不是批量任意选择删除!
{
OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString); //创建连接对象
conn.Open();
if (Request.Form["chkid"] != null)
{
string sqldel = "select * from links where id in (" + Request.Form["chkid"] + ") order by id desc";
OleDbCommand cmddel = new OleDbCommand(sqldel, conn); //执行查询语句
OleDbDataReader sdr = cmddel.ExecuteReader();
if (sdr.Read())
{
foreach (int i in Request.Form["chkid"].Length)
{
FileInfo file = new FileInfo(Server.MapPath("~/" + sdr["pic"].ToString()));
file.Delete();
}
string sql = "Delete from links where id in (" + Request.Form["chkid"] + ")";
OleDbCommand cmd = new OleDbCommand(sql, conn); //执行删除语句
cmd.ExecuteNonQuery();
cmd.Dispose();
Response.Write("<script>alert('系统提示:选中记录已删除成功!');window.location.href='Manage_links.aspx';</script>");
}
else
{
Response.Write("<script>alert('系统提示:数据库暂无记录!');window.location.href='Manage_links.aspx;</script>");
}
cmddel.Dispose();
sdr.Close();
}
else
{
Response.Write("<script>alert('系统提示:您还未选中想要删除的记录!');window.history.go(-1);</script>");
}
conn.Close();
}注,这个问题是一个DataList里checkbox全选,传递ID,然后想批量删除数据库记录及上传的图片,重点是删除图片,现在只能一张一张的删除,而不是批量任意选择删除!
对你来说,你只要界面选择的ID记录到一个数组,然后用个for循环即可!
for (int i = imgNum-1; i >-1 ; i--) {
if (ViewState["cb" + i.ToString()] != null)
{
dr = dt.Rows;
string 文物图片_ID = dr["文物图片编号"].ToString(); if (文物图片.Delete(文物图片_ID).Vlus) this.Panel1.Controls.Add(new LiteralControl("<font color=red>删除图片成功!</font><br>")); else this.Panel1.Controls.Add(new LiteralControl("<font color=red>删除图片失败,数据库操作发生错误!!</font>")); }