0

当我在表中插入一条记录时,在“状态”列中插入“A”。
现在我想在选中复选框并单击删除按钮后自动将“A”更改为“N”。

你能帮我么?

string constr = ConfigurationManager.ConnectionStrings["webConnectionString"].ConnectionString;

protected void btnDelete_Click(object sender, EventArgs e)
{
   foreach (GridViewRow row in GridView1.Rows) {

        CheckBox chk = (CheckBox)row.FindControl("chkSelect");
        if (chk.Checked == true)
        {
            int cnt= int.Parse(GridView1.DataKeys[row.RowIndex].Value.ToString());
            SqlConnection con = new SqlConnection(constr);
            string sql = "delete from PA_webwork where sno = @sno ";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@sno", cnt);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
   GridView1.DataBind();
}

   database     :: web
   table name   :: PA_webwork

   column names ::

            sno         :: int
            article no  :: varchar(50)
            state       :: varchar(50)
            date        :: smalldatetime
4

1 回答 1

1

你可以写更新语句

            int cnt= int.Parse(GridView1.DataKeys[row.RowIndex].Value.ToString());
            SqlConnection con = new SqlConnection(constr);
            string sql = "update  PA_webwork set state 'N'  where sno = @sno ";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@sno", cnt);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
于 2013-08-26T09:12:00.920 回答