示例#1
0
        protected void BtnDel_Click(object sender, EventArgs e)
        {
            string sqlText = "delete from tb_inf where id=@id";
            string connStr = WebConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chkSel = (CheckBox)row.FindControl("ChkSel");
                if (chkSel.Checked)
                {
                    using (SqlConnection conn = new SqlConnection(connStr))
                    {
                        SqlCommand cmd = conn.CreateCommand();
                        cmd.CommandText = sqlText;
                        cmd.Parameters.AddWithValue("@id", row.Cells[1].Text);
                        conn.Open();
                        int rowsAffected = cmd.ExecuteNonQuery();
                        conn.Close();

                        if (rowsAffected > 0)
                        {
                            StrHelper.Alert("删除成功!");
                        }
                        else
                        {
                            StrHelper.Alert("删除失败!");
                        }
                    }
                }
            }

            DisplayData();
        }
示例#2
0
        protected void BtnUpd_Click(object sender, EventArgs e)
        {
            string sqlText = "Update tb_inf set issueDate=@UpdateTime where id=@id";
            string connStr = WebConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                CheckBox chkSel = (CheckBox)GridView1.Rows[i].FindControl("ChkSel");
                if (chkSel != null)
                {
                    if (chkSel.Checked)
                    {
                        using (SqlConnection conn = new SqlConnection(connStr))
                        {
                            SqlCommand cmd = conn.CreateCommand();
                            cmd.CommandText = sqlText;
                            cmd.Parameters.AddWithValue("@UpdateTime", DateTime.Now);
                            cmd.Parameters.AddWithValue("@id", GridView1.Rows[i].Cells[1].Text);
                            conn.Open();
                            int rowsAffected = cmd.ExecuteNonQuery();
                            conn.Close();

                            if (rowsAffected > 0)
                            {
                                StrHelper.Alert("更新成功!");
                            }
                            else
                            {
                                StrHelper.Alert("更新失败!");
                            }
                        }
                    }
                }
            }

            DisplayData();
        }