C# OleDB Delete Command

2019-07-20 21:40发布

问题:

I am currently creating an window application connected with a Microsoft Access database to perform CRUD operation for the business related to education

One of the workflow is to search the keyword and perform CRUD operation in the DataGrid View At first , I am trying to display the datagrid view of the record ( such as students , time to take course, what course...e.t.c.) with respect to the receipt number

The application logic is to get the receipt number in the datagrid View and perform delete operation

So my question is

  1. How to get the value of the receipt number column in the selected row of the datagrid view?

  2. There are OleCommands and OleDataAdapter to perform the CRUD operation. Which method shall I use ?

The following is the code for the delete operation

        public void delete_course_transaction(string receipt_no)
    {
        OleDbDataAdapter oledbAdapter = new OleDbDataAdapter();
        try
        {

            using (OleDbConnection connection = new OleDbConnection(connectionDBString))
            {
                string sql = "delete from COURSE_TAKE where COURSE_TAKE.RECEIPT_NO = '" + receipt_no + "'";
                connection.Open();
                oledbAdapter.DeleteCommand = connection.CreateCommand();
                oledbAdapter.DeleteCommand.CommandText = sql;
                int rows = oledbAdapter.DeleteCommand.ExecuteNonQuery();
                if (rows > 0)
                {
                    MessageBox.Show("Delete Course transaction Success!");
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }

回答1:

If I understand correctly :

for your first question you can use this code :

txtName.Text = dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString();

and your second question : refer here :http://forums.asp.net/t/706106.aspx/1