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,然后想批量删除数据库记录及上传的图片,重点是删除图片,现在只能一张一张的删除,而不是批量任意选择删除!
可以建一个临时列表,把所有的SQL语句(删除语句)放到列表中,然后用事务进行批量删除。简单代码参考:
List<string> sqlList=new List<string>();
string sql = "Delete from links where id in (" + Request.Form["chkid"] + ")";
sqlList.Add(sql);
循环完成后,
cmddel=new OleDbCommand ();
cmddel.Connection = conn;
OleDbTransaction tx=conn.BeginTransaction();
cmddel.Transaction=tx;
try
{
for (int n = 0; n < sqlList.Count; n++)
{
string sql=sqlList[n].ToString();
if(sql.Trim().Length>1)
{
cmd.CommandText=sql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (Exception ex)
{
tx.Rollback();
}
我的目的是批量物理删除上传的图片,上面的代码没有删除语句呀,能否明示,有具体代码吗?这个问题做了好长时间,没有做出来,谢谢
@沈大荣:我写的代码中的第二句就是你的删除语句啊
@沈大荣:
你可以把所有ID(checkbox全选,传递ID)用逗号隔开,作为一个字符串传递过来,如传递过来为 chkids,
sting[] listid=chkids.spit(',');
for(int i=0;i<listid.length;i++)
{
string sql = "Delete from links where id in (" + Request.Form["chkid"] + ")";
sqlList.Add(sql);
}
然后接上面的代码就可以的了
@Apple丫头:你是高手,能否给个完整的代码,我拜你为师,刚从asp转向.net,有很多不懂的
173844862 .Net高级技术交流群 有北大专家指导
@Apple丫头:你有QQ吗?
@沈大荣: if (sdr.Read())
{
List<string> sqlList = new List<string>();
string chkids = Request.Form["chkid"];
string[] listid = chkids.Split(',');
for (int i = 0; i < listid.Length; i++)
{
FileInfo file = new FileInfo(Server.MapPath("~/" + sdr["sqlList"].ToString()));
file.Delete();
string sql = "Delete from links where id in (" + chkids + ")";
OleDbCommand cmd = new OleDbCommand(sql, conn); //执行删除语句
cmd.ExecuteNonQuery();
cmd.Dispose();
sqlList.Add(sql);
}
Response.Write("<script>alert('系统提示:选中记录已删除成功!');window.location.href='Manage_links.aspx';</script>");
}
物理删除图片语句,还是删除不了图片,下标要怎么写
@沈大荣:有完整的代码吗?发个给我,谢谢,比较急
@沈大荣:
if (sdr.Read())
{
List<string> sqlList = new List<string>();
string chkids = Request.Form["chkid"];
string[] listid = chkids.Split(',');
for (int i = 0; i < listid.Length; i++)
{
FileInfo file = new FileInfo(Server.MapPath("~/" + sdr[listid[i]].ToString()));
file.Delete();
string sql = "Delete from links where id in=listid[i] ";
sqlList.Add(sql);
}
cmddel.Connection = conn;
OleDbTransaction tx=conn.BeginTransaction();
cmddel.Transaction=tx;
try
{
for (int n = 0; n < sqlList.Count; n++)
{
string sql=sqlList[n].ToString();
if(sql.Trim().Length>1)
{
cmd.CommandText=sql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (Exception ex)
{
tx.Rollback();
}
Response.Write("<script>alert('系统提示:选中记录已删除成功!');window.location.href='Manage_links.aspx';</script>");
}
@Apple丫头:在,运行到这里:FileInfo file = new FileInfo(Server.MapPath("~/" + sdr[listid[i]].ToString()));
出错了
@沈大荣:数据库的图片字段是pic字段,改了后,还是出错
@沈大荣:搞定了,谢谢
@沈大荣:
代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString); //创建连接对象
conn.Open();
if (Request.Form["chkid"] != null)
{
string chkids = Request.Form["chkid"];
string[] listid = chkids.Split(',');
for (int i = 0; i < listid.Length; i++)
{
string sqlsearch = "select * from links where id=(" + listid[i] + ") order by id desc";
OleDbCommand cmdserach = new OleDbCommand(sqlsearch, conn); //执行查询语句
OleDbDataReader sdr = cmdserach.ExecuteReader();
if (sdr.Read())
{
FileInfo file = new FileInfo(Server.MapPath("~/" + sdr["pic"].ToString()));
file.Delete();
string sql = "Delete from links where id=" + listid[i] + "";
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>");
}
cmdserach.Dispose();
sdr.Close();
}
}
else
{
Response.Write("<script>alert('系统提示:您还未选中想要删除的记录!');window.history.go(-1);</script>");
}
conn.Close();
}
两张关联表,一个是产品类别表,一个是产品表.
.net中,我用datalist控件,datalist控件里一个label控件,AspNetPager1,进行分页显示,问题是我怎样在产品类别页显示(根据类别表的id统计该类别下产品的总数),即在对应的产品类别中显示有多少个产品.
这个用什么方法实现,谢谢!下面代码实现不了
protected void BindData()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString); //创建连接对象
conn.Open();
string sql = "select * from NM_Countdown_category order by px asc";
SqlCommand cmd = new SqlCommand(sql, conn); //执行查询语句
DataSet ds = new DataSet();
SqlDataAdapter oda = new SqlDataAdapter(cmd);
oda.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "NM_Countdown_category");
DataList1.DataSource = ds.Tables["NM_Countdown_category"].DefaultView;
DataList1.DataBind();
string sqlsearch;
for (int i = 0; i < DataList1.Items.Count; i++)
{
sqlsearch = "select count(*) from NM_Countdown_products where lb='" + DataList1.DataKeyField + "'";
SqlCommand cmdsearch = new SqlCommand(sqlsearch, conn); //执行查询语句
Label Tongji = (Label)DataList1.Items[i].FindControl("Label1");
Tongji.Text = cmdsearch.ExecuteScalar().ToString();
}
cmd.Dispose();
ds.Dispose();
oda.Dispose();
conn.Close();
}
类别表字段:lb,px,xianshi,tm,id
产品表字段:cpname,pic,content,stock,lbid,id
@沈大荣: 楼主结贴呀!