C# OleDB Delete Command

ε祈祈猫儿з 提交于 2019-12-24 00:59:08

问题


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



来源:https://stackoverflow.com/questions/13656140/c-sharp-oledb-delete-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!