如何在 C# 中使用 using 语句进行错误处理

发布于 2024-12-13 01:54:56 字数 500 浏览 1 评论 0原文

我正在尝试从多个数据库表中删除记录。对于错误处理,我使用 try/catch 块,如下所示。

try
{
    using (SqlCeConnection oConn = new SqlCeConnection(ConnectionString))
    {
        oConn.Open();
        using (SqlCeTransaction transaction = oConn.BeginTransaction())
        {
            //delete from multiple tables using ADO.NET
            transaction.Commit();
        }
    }
}
catch
{
   //error handling
}

问题是当引发异常时事务不会回滚。在阅读多个论坛时,我的结论是“使用”语句应该处理事务和连接。处置时,应回滚未提交的事务。

有人可以告诉我我做错了什么吗?

im trying to do delete records from multiple database tables. For error handling i'm using a try/catch block as seen beneath.

try
{
    using (SqlCeConnection oConn = new SqlCeConnection(ConnectionString))
    {
        oConn.Open();
        using (SqlCeTransaction transaction = oConn.BeginTransaction())
        {
            //delete from multiple tables using ADO.NET
            transaction.Commit();
        }
    }
}
catch
{
   //error handling
}

problem is when an exception is raised the transaction is not rolled back. When reading multiple forums my conclusion is that 'using' statements should dispose the transaction and the connection. When disposed, the uncommitted transaction should be rolled back.

Can someone tell me what I'm doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何以笙箫默 2024-12-20 01:54:56

我的猜测是您没有设置 SqlCeCommandTransaction 属性(您没有显示)。

try
{
    using (SqlCeConnection oConn = new SqlCeConnection(ConnectionString))
    {
        oConn.Open();
        using (SqlCeCommand command = oConn.CreateCommand())
        {
            using (SqlCeTransaction transaction = oConn.BeginTransaction())
            {
               command.Transaction = transaction;
               //delete from multiple tables using ADO.NET using command
               transaction.Commit();
            }
        }

    }
}
catch
{
   //error handling
}

SqlCeCommand 不会自动加入事务,您必须显式设置它。据我所知,TransactionDispose() 应该将其回滚,但您可以自己调用 Rollback() 作为其他人建议只是为了确定。

My guess is that you're not setting the Transaction property of your SqlCeCommand (which you don't show).

try
{
    using (SqlCeConnection oConn = new SqlCeConnection(ConnectionString))
    {
        oConn.Open();
        using (SqlCeCommand command = oConn.CreateCommand())
        {
            using (SqlCeTransaction transaction = oConn.BeginTransaction())
            {
               command.Transaction = transaction;
               //delete from multiple tables using ADO.NET using command
               transaction.Commit();
            }
        }

    }
}
catch
{
   //error handling
}

The SqlCeCommand won't automatically enlist in the transaction, you have to set it explicitly. As far as I'm aware, the Dispose() of the Transaction should roll it back, but you can call Rollback() yourself as others suggest just to be sure.

抱着落日 2024-12-20 01:54:56
try 
{
  using (SqlCeConnection oConn = new SqlCeConnection(ConnectionString))
  {
    oConn.Open();

    using (SqlCeTransaction transaction = oConn.BeginTransaction())
    {
        try
        {
          //delete from multiple tables using ADO.NET
          transaction.Commit();
        }
        catch 
        {
           transaction.Rollback();
           throw;
        }
    }
  }
}
catch (Exception e)
{
  // do Exception handling here
}
try 
{
  using (SqlCeConnection oConn = new SqlCeConnection(ConnectionString))
  {
    oConn.Open();

    using (SqlCeTransaction transaction = oConn.BeginTransaction())
    {
        try
        {
          //delete from multiple tables using ADO.NET
          transaction.Commit();
        }
        catch 
        {
           transaction.Rollback();
           throw;
        }
    }
  }
}
catch (Exception e)
{
  // do Exception handling here
}
星光不落少年眉 2024-12-20 01:54:56

为了安全起见:

    using (SqlCeTransaction transaction = oConn.BeginTransaction())
    {
        try
        {
          //delete from multiple tables using ADO.NET
          transaction.Commit();
        }
        catch
        {
           transaction.Rollback();
           throw;
        }
    }

我找不到直接说明事务默认回滚的文档。

To play it safe:

    using (SqlCeTransaction transaction = oConn.BeginTransaction())
    {
        try
        {
          //delete from multiple tables using ADO.NET
          transaction.Commit();
        }
        catch
        {
           transaction.Rollback();
           throw;
        }
    }

I couldn't find a direct piece of documentation that states a transaction is rolled back by default.

倒带 2024-12-20 01:54:56

基本上,您的 try{...} catch{} 块位于错误的位置。它应该在 using 语句中,最好在 transaction.Commit 周围

Basically your try{...} catch{} block is in the wrong place. It should be within the using statement, preferably around the transaction.Commit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文