【问题标题】:exception in sqlite transaction c#sqlite事务c#中的异常
【发布时间】:2013-12-18 20:31:01
【问题描述】:

我尝试了所有方法,但总是在“com.Transaction = trx;”处返回 System.NullreferencesException

   public override int ExecuteNonQuery(string query)
    {

        int register=0;
        SQLiteTransaction trx=null;
        SQLiteCommand com;
        try
        {
            if (this.IsClosed()) this.Open();

                trx = con.BeginTransaction();
                com = new SQLiteCommand(query, con);
                com.Transaction = trx;
                register = com.ExecuteNonQuery();
                trx.Commit();

            return register;
        }
        catch (SQLiteException ex)
        {
            trx.Rollback();//se tiene q deshaser toda la trransaccion hecha
            throw ex;
        }

        finally
        {
            this.Close();
        }

    }

我不知道我的错误在哪里:(

这是在transaction 中引发我的异常

更新:

这里是Exception Detalis

【问题讨论】:

  • 这是精简版吗?因为您发布的内容不需要明确的交易。
  • 奇怪的是你得到这个“com.Transaction = trx;”的异常线。你能发布完整的堆栈跟踪吗?
  • 我只是上传了异常,是的,我不知道为什么事务为空:(
  • 可能是版本?,但我在哪里看到,在参考 System.Data.Sqlite 版本说 1.0.89.0
  • @RobertoMassimoPataraBrianco 你能在这里复制粘贴完整的(文本)堆栈跟踪吗?您链接到的图像很好,但它不是堆栈跟踪。我的意思是“查看详细信息”下的那个,它应该被称为“堆栈跟踪”。

标签: c# database transactions command executenonquery


【解决方案1】:

尝试替换这些行

 if (this.IsClosed()) 
      this.Open();

 if (con.State == ConnectionState.Closed) 
     con.Open();

【讨论】:

  • 我刚刚做到了,并在“com.Transaction = trx;”处不断向我抛出 System.NullreferencesException :(
  • 真的这个问题看起来很奇怪。我正在查看 SQLiteCommand 的 Transaction 属性的源代码,当您尝试分配一个值时,如果该值为空,则没有任何东西会引发 NRE 异常。
  • 值“trx”是连接打开的,不为空
【解决方案2】:

我修复了它,问题出在事务上,我认为 sqlite 不支持它,我评论了它,一切都像魅力一样。

  public override int ExecuteNonQuery(string query)
    {

        int register=0;
        //SQLiteTransaction trx=null;
        SQLiteCommand com;
        try
        {
            if (con.State == ConnectionState.Closed) this.Open();

                //trx = con.BeginTransaction();//para hacer transaciones para que la base de datos este estable
                com = new SQLiteCommand(query, con);
                //com.Transaction = trx;
                register = com.ExecuteNonQuery();//recien hacemos la coneccion en esta linea
               // trx.Commit();

            return register;
        }
        catch (SQLiteException ex)
        {
           // trx.Rollback();//se tiene q deshaser toda la trransaccion hecha
            throw ex;
        }

        finally
        {
            this.Close();
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 2010-11-01
    相关资源
    最近更新 更多