//button2事件
protected void Button2_Click(object sender, EventArgs e)
{
//遍历gridview表格
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
//查找checkbox1复选框
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
//如果选中
if (cbox.Checked == true)
{
//数据库连接工厂,获取单例
ConnectionFactory cf = ConnectionFactory.getInstance();
//获取数据库连接
SqlConnection con = cf.getConnection();
//数据库执行语句
string sqlstr = "delete from CollectionCars where Series=@series";
//数据库执行对象
SqlCommand cmd = new SqlCommand(sqlstr, con);
//添加数据库参数
cmd.Parameters.Add("@series", SqlDbType.NChar, 10).Value = ((HyperLink)GridView1.Rows[i].Cells[2].Controls[0]).Text.Trim().ToString();
//打开数据库连接
con.Open();
//执行查询
cmd.ExecuteNonQuery();
//绑定数据
bind();
//关闭数据库连接
con.Close();
}
这就是一个批量删除数据的操作!
protected void Button2_Click(object sender, EventArgs e)//点击按钮执行一下操作!
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)//遍历gridview中的每一行!
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");//获取当前行选择框checkbox1
if (cbox.Checked == true)//如果选择框被选中
{
ConnectionFactory cf = ConnectionFactory.getInstance();//获取数据库实例
SqlConnection con = cf.getConnection();//设置数据库链接
string sqlstr = "delete from CollectionCars where Series=@series";//初始化删除语句
SqlCommand cmd = new SqlCommand(sqlstr, con);//建立数据库链接
cmd.Parameters.Add("@series", SqlDbType.NChar, 10).Value = ((HyperLink)GridView1.Rows[i].Cells[2].Controls[0]).Text.Trim().ToString();//为删除语句当中的变量赋值
con.Open();//打开链接
cmd.ExecuteNonQuery();//执行删除
bind();//重新填充gridview
con.Close();//关闭链接!
}