ConstraintException没有被捕获

问题描述 投票:0回答:2

第一次海报。我在这里享受了多年的帮助。感谢大家。

我的情况看起来应该不会发生。

在VS2017社区中使用VB.NET,我在Try块中得到一个System.Data.ConstraintException,其中我特意捕获了该确切的异常。

这是消息的样子:

System.Data.ConstraintException:'Column'PAIR1,PAIR2,PAIR3'被约束为唯一的。价值'CHATBTC,ETHBTC,CHATETH'已经存在。

https://www.dropbox.com/s/d91rgtwsjwioqhm/SO_error.jpg?dl=0

正如您可以通过逻辑判断的那样,我指望触发异常以便我可以构建一个包含唯一行的表并添加到我的重复行值。随着表的大小增加,在ADD之前检查重复项需要花费大量时间,因此这种方法是最快的。

它不会每次都发生,只有约30%。我的应用程序在生产中运行还不够,所以我看到的一切都是在调试时。

我的代码在这里:

  tblTriArbPairs.PrimaryKey = New DataColumn() {tblTriArbPairs.Columns("PAIR1"), tblTriArbPairs.Columns("PAIR2"), tblTriArbPairs.Columns("PAIR3")}

  Try
      tblTriArbPairs.Rows.Add(
    Pairs(0), Pairs(1), Pairs(2),
    idxPair0, idxPair1, idxPair2,
    result.TD1, result.TD2, result.TD3,
    CoinOnly(Pairs(0)), CurrOnly(Pairs(0)),
    CoinOnly(Pairs(1)), CurrOnly(Pairs(1)),
    CoinOnly(Pairs(2)), CurrOnly(Pairs(2)),
    FindLoopCoin(CoinOnly(Pairs(0)), CurrOnly(Pairs(0)), CoinOnly(Pairs(1)), CurrOnly(Pairs(1)), CoinOnly(Pairs(2)), CurrOnly(Pairs(2))),
    GetSymbolLIQ(Pairs(0)), GetSymbolLIQ(Pairs(1)), GetSymbolLIQ(Pairs(2))
    )
      RowsAdded += 1
  Catch ex As System.Data.ConstraintException
      DupRows += 1
  Catch ex As Exception
  Finally
  End Try

当填充表时,我最终添加了3480行和2640个重复项。发生错误时没有一致性。有时候马上,有时几乎在最后。

我看了一遍,没有发现任何解决ConstraintException没有被捕获的东西。其他例外,是的。

很感谢任何形式的帮助。希望我发布了一个很好的问题。 :)

vb.net constraintexception
2个回答
0
投票

我已经读过,捕获异常是指导程序流的一种相当麻烦的方式,最好是防止异常。

我认为这是你正在处理的数据表,所以下面的Linq可能会做到这一点。根据文档“一旦结果可以确定,就会停止对源的枚举。”

我刚用我的样本数据库进行测试。

    Dim CoffeeName As String = "Black Tiger"
    Dim CoffeeType = "Decaf"

    Dim dup As Boolean = dt.AsEnumerable().Any(Function(Row) CoffeeName = Row.Field(Of String)("Name") And CoffeeType = Row.Field(Of String)("Type"))
    MessageBox.Show(dup.ToString)

编辑用您的变量和3个字段编写的代码。

    Dim dup As Boolean = tblTriArbPairs.AsEnumerable().Any(Function(Row) Pairs(0) = Row.Field(Of String)("Pair1") _
        And Pairs(1) = Row.Field(Of String)("Pair2") And Pairs(2) = Row.Field(Of String)("Pair3"))
    If dup Then
        DupRows += 1
        MessageBox.Show("Sorry, duplicate")
        Exit Sub
    End If
    'Add the row

0
投票

我没有解决过为什么抛出异常的问题,尽管我明确地陷入了困境。在尝试将它们添加到我的数据表之前,我注意了这里的建议以检查是否存在重复项。这是我提出的,似乎工作得很好。

Dim iRow As DataRow() = tblTriArbPairs.Select("PAIR1 = '" & Pairs(0) & "' AND PAIR2 = '" & Pairs(1) & "' AND PAIR3 = '" & Pairs(2) & "'")
If iRow.Count = 0 Then
    tblTriArbPairs.Rows.Add(
      Pairs(0), Pairs(1), Pairs(2),
      idxPair0, idxPair1, idxPair2,
      result.TD1, result.TD2, result.TD3,
      CoinOnly(Pairs(0)), CurrOnly(Pairs(0)),
      CoinOnly(Pairs(1)), CurrOnly(Pairs(1)),
      CoinOnly(Pairs(2)), CurrOnly(Pairs(2)),
      FindLoopCoin(CoinOnly(Pairs(0)), CurrOnly(Pairs(0)), CoinOnly(Pairs(1)), CurrOnly(Pairs(1)), CoinOnly(Pairs(2)), CurrOnly(Pairs(2))),
      GetSymbolLIQ(Pairs(0)), GetSymbolLIQ(Pairs(1)), GetSymbolLIQ(Pairs(2))
      )
    RowsAdded += 1
Else
    DupRows += 1
End If

再次感谢所有帮助过的人。我是通过我的第一个问题来完成的!是啊!

© www.soinside.com 2019 - 2024. All rights reserved.