如何在EXCEL工作表数据库中删除行并且工作表在C#中已更新 [英] How to delete row in EXCEL sheet database and sheet is update in C#

查看:57
本文介绍了如何在EXCEL工作表数据库中删除行并且工作表在C#中已更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据在excel中可用,但是当我尝试用此代码删除一行时
'''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''

The data is available in excel but when I tryy to delete a row with this code
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

conn = new OleDbConnection();
                       conn.ConnectionString = gm.ConnectionString;
                       conn.Open();
                       cmd = new OleDbCommand("select * from [ExecutiveMember]", conn);
                       da = new OleDbDataAdapter(cmd);
                       dt.Clear();
                       da.Fill(dt);
                       DataRow dtRowDelete = dt.Rows[index];
                       dtRowDelete.Delete();
                       dt.AcceptChanges();
                       //DataTable dtable = dt;
                       da.Update(dt);

                       dgvExecutiveMember.DataSource = dt;



                       //cmd.Connection = conn;
                       //cmd.CommandText = "DELETE from [ExecutiveMember] where MobileNumber='" + MobileNumber + "'";
                       cmd.ExecuteNonQuery();
                       conn.Close();
                       Clear();
                       MobileNumber = "";

                       // index = -1;
                       MessageBox.Show("Data Deleted!...");
                        //conn = new OleDbConnection();
                        //conn.ConnectionString = gm.ConnectionString;
                        //conn.Open();
                        //string delete = "delete from ExecutiveMember where MobileNumber = '+91''" + txtMobile2.Text + "'";
                        //cmd = new OleDbCommand(delete, conn);
                        //cmd.ExecuteNonQuery();
                        //conn.Close();
                        //MessageBox.Show("Data Deleted!...");


'''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''' ``''''''''''''''''''''''''''''''''


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

row is delete but in excel sheet data is present.

推荐答案

似乎主数据源(在您的情况下为Excel)没有向事务获取提交命令.但是更改反映在InMemory临时存储中.可能的原因可能是执行期间出现一些未处理的异常.

作为一种永久的解决方案/最佳实践模式,我们可以将以下带有TransactionScope的编码样式用于任何数据源连接/执行.

It looks like primary source (in your case Excel) is not getting the commit command to the transaction. But the changes are reflected at InMemory temporary storage. Possible cause could be some unhandled exception during the execution.

As a permanent solution/best practice mode, we can use the below style of coding with TransactionScope for any data source connection/execution.

try
{
    conn = new OleDbConnection();
    conn.ConnectionString = gm.ConnectionString;
    conn.Open();
    transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted);

    cmd = new OleDbCommand("select * from [ExecutiveMember]", conn);
    .................
    .................

    cmd.ExecuteNonQuery();
    transaction.Commit();
    conn.Close();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
    transaction.Rollback();
}


这篇关于如何在EXCEL工作表数据库中删除行并且工作表在C#中已更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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