删除操作的一个问题
我想在WIN中进行一个删除操作,点击删除按纽,弹出一个对话框,要求是否进行删除,点击是则删除,点击否则取消。这个操作怎么实现?最好有代码
------解决方案--------------------private void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection conn = ManageConn.getConnection();
string sql= "delete from PGSubMenu where sId=@sId ";
SqlCommand cmd=new SqlCommand();
string message = "你確定要刪除你所選擇的數據?刪除後數據不可恢復﹗ ";
string caption = "警告 ";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
result = MessageBox.Show(this, message, caption, buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (result == DialogResult.Yes)
{
try
{
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.Parameters.Add( "@sId ", SqlDbType.NChar, 40, "sId ");
cmd.Parameters[ "@sId "].Value = dgvSubMenu.CurrentRow.Cells[1].Value.ToString();
cmd.ExecuteNonQuery();
ManageConn.freeConnection(conn);
RefreshDataGrid();
}
catch (SqlException e1)
{
MessageBox.Show( "錯誤訊息: " + e1.Message, "錯誤提示 ");
}
catch (Exception e2)
{
MessageBox.Show( "錯誤訊息: " + e2.Message, "錯誤提示 ");
}
finally
{
ManageConn.freeConnection(conn);
ModifyStatus = false;
}
}
}