当前位置: 编程技术>.net/c#/asp.net
asp.net批量删除实例代码教程
来源: 互联网 发布时间:2014-08-30
本文导语: asp.net实现批量删除的方法 有关asp.net实现批量删除功能的方法。 .aspx文件代码: 暂时无数...
asp.net实现批量删除的方法
有关asp.net实现批量删除功能的方法。
.aspx文件代码:
.cs 文件代码:
protected void btndeleteall_Click(object sender, EventArgs e)
{
string sqltext = "(";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chb = (CheckBox)GridView1.Rows[i].FindControl("id");
if (chb.Checked)
{
sqltext = sqltext + GridView1.DataKeys[i].Value.ToString() + ",";
}
}
sqltext = sqltext.Substring(0, sqltext.Length - 1) + ")";
sqltext = "delete from shangpu where id in" + sqltext;
string sqlcon = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection con = new SqlConnection(sqlcon);
con.Open();
SqlCommand cmd = new SqlCommand(sqltext, con);
try
{
int count = Convert.ToInt32(cmd.ExecuteNonQuery());
if (count > 0)
{
viewbind();
MessageBox.Show(this, "删除成功,共删除" + count + "条记录!");
}
}
catch
{
MessageBox.Show(this, "删除失败!");
}
finally
{
con.Close();
con.Dispose();
}
}
{
string sqltext = "(";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chb = (CheckBox)GridView1.Rows[i].FindControl("id");
if (chb.Checked)
{
sqltext = sqltext + GridView1.DataKeys[i].Value.ToString() + ",";
}
}
sqltext = sqltext.Substring(0, sqltext.Length - 1) + ")";
sqltext = "delete from shangpu where id in" + sqltext;
string sqlcon = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection con = new SqlConnection(sqlcon);
con.Open();
SqlCommand cmd = new SqlCommand(sqltext, con);
try
{
int count = Convert.ToInt32(cmd.ExecuteNonQuery());
if (count > 0)
{
viewbind();
MessageBox.Show(this, "删除成功,共删除" + count + "条记录!");
}
}
catch
{
MessageBox.Show(this, "删除失败!");
}
finally
{
con.Close();
con.Dispose();
}
}