C#OLEDB Delete命令 [英] C# OleDB Delete Command

查看:975
本文介绍了C#OLEDB Delete命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建与Microsoft Access数据库连接到相关教育业务执行CRUD操作的窗口应用程序



其中的工作流程是搜索关键字并在DataGrid中查看
执行CRUD操作起初,我试图显示记录的数据网格视图(如学生,及时采取当然,何去何从...等)相对于收据号



应用程序逻辑是获得在DataGrid查看收据号码,并执行删除操作。



所以我的问题是




  1. 如何获得数据网格视图的选择行中的收据编号列的值?


  2. 有OleCommands和OleDataAdapter执行CRUD操作。我将使用哪种方法?




以下是删除操作。



<$代码p $ p> 公共无效delete_course_transaction(字符串receipt_no)
{
OleDbDataAdapter的oledbAdapter =新OleDbDataAdapter的();

{

使用(OleDbConnection的连接=新的OleDbConnection(connectionDBString))
{
字符串SQL =从COURSE_TAKE删去COURSE_TAKE.RECEIPT_NO = '+ receipt_no +';
connection.Open();
oledbAdapter.DeleteCommand = connection.CreateCommand();
oledbAdapter.DeleteCommand.CommandText = SQL;
INT行= oledbAdapter.DeleteCommand.ExecuteNonQuery();
如果(行大于0)
{
MessageBox.Show(删除课程交易成功!);
}
}
}
赶上(例外五)
{
MessageBox.Show(e.Message);
}
}


解决方案

如果我理解正确的:



关于第一个问题,你可以使用此代码:

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

和你的第二个问题:
参考这里:的 http://forums.asp.net/t/706106.aspx/1


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);
        }
    }

解决方案

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

这篇关于C#OLEDB Delete命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆