从C#删除数据库表中的操作不起作用

时间:2013-10-06 07:25:31

标签: c# .net oracle

我正在使用Oracle 10g Express Edition。

根据表格中的值,动态生成一组radiobuttons和文本框。

我想要的是当用户选择单选按钮并点击删除按钮时,无线电按钮前面文本字段中的值将被捕获到字符串变量中,然后在表中删除该值。数据库。

这就是我生成radiobuttons和文本框的方式:

foreach (DataRow dr in dsData.Tables[0].Rows){

    TextBox txt_task = new TextBox();
    txt_task.Enabled = false;
    txt_task.Text = " " + dr[0].ToString();
    txt_task.Width = 300;
    txt_task.ID = "dynamictask" + counter.ToString();

    RadioButton chk_delete = new RadioButton();
    chk_delete.Enabled = true;
    chk_delete.GroupName = "del_grp";
    chk_delete.ID = "dynamicrdio" + counter.ToString();

    dynamic_controls_holder.Controls.Add(chk_delete);
    dynamic_controls_holder.Controls.Add(new LiteralControl("   "));
    dynamic_controls_holder.Controls.Add(txt_task);
    dynamic_controls_holder.Controls.Add(new LiteralControl("<br />"));

    //The inital value of counter was 0.
    counter+=1;
}

这就是我尝试执行删除操作的方式:

for (int counter = 0; counter <= rows_count; counter++){
    string dynamic_rdio_finder = "dynamicrdio" + counter.ToString();
    string dt = "dynamictask" + counter.ToString();
    RadioButton temp_rdio = (RadioButton)dynamic_controls_holder.FindControl(dynamic_rdio_finder);
    if (temp_rdio.Checked){
        TextBox temp_task = (TextBox)dynamic_controls_holder.FindControl(dt);
        temp_task_txt = temp_task.Text;
        break;
    }
}

connection.Open();
OracleCommand command = new OracleCommand("DELETE FROM tbl_mark6_task WHERE description_task=:description_task", connection);
command.Parameters.AddWithValue("description_task", temp_task_txt);
rows_count = command.ExecuteNonQuery();
command.Parameters.Clear();

if (rows_count == 1){
    lbl_notification.Text = "Task was deleted successfully.";
}
else if (rows_count != 1){
    lbl_error.Text = "Task was not deleted!";
}

这不起作用。我似乎无法找到问题所在。

1 个答案:

答案 0 :(得分:1)

您需要在transaction边界使用它,并且需要显式提交它。可在此link

中找到更多信息

另请注意,OracleCommand现已弃用。它将在下一版本中删除。

OracleTransaction transaction;
command.Transaction = transaction;
....
command.ExecuteNonQuery();
transaction.Commit();