5

当我在子句NULL中显式过滤掉这些行时,为什么以下查询会针对具有桶值的行引发以下错误?Where

Dim query = From row As dbDataSet.conformalRow In dbDataSet.Tables("conformal") _
            Where Not IsDBNull(row.Cal) AndAlso tiCal_drop.Text = row.Cal _
            AndAlso Not IsDBNull(row.Tran) AndAlso tiTrans_drop.Text = row.Tran _
            AndAlso Not IsDBNull(row.barrel) _
            Select row.barrel
If query.Count() > 0 Then tiBarrel_txt.Text = query(0)

Run-time exception thrown : System.Data.StrongTypingException - The value for column 'barrel' in table 'conformal' is DBNull.

应该如何重写我的查询/条件以按预期工作?

4

2 回答 2

7

默认情况下,在强类型数据集中,如果字段为空,属性会抛出该异常。您需要使用生成的Is[Field]Null方法:

Dim query = From row As dbDataSet.conformalRow In dbDataSet.Tables("conformal") _
            Where Not row.IsCalNull() AndAlso tiCal_drop.Text = row.Cal _
            AndAlso Not row.IsTranNull() AndAlso tiTrans_drop.Text = row.Tran _
            AndAlso Not row.IsbarrelNull() _
            Select row.barrel
If query.Count() > 0 Then tiBarrel_txt.Text = query(0)

或 DataRow.IsNull 方法:

Dim query = From row As dbDataSet.conformalRow In dbDataSet.Tables("conformal") _
            Where Not row.IsNull("Cal") AndAlso tiCal_drop.Text = row.Cal _
            AndAlso Not row.IsNull("Tran") AndAlso tiTrans_drop.Text = row.Tran _
            AndAlso Not row.IsNull("barrel") _
            Select row.barrel
If query.Count() > 0 Then tiBarrel_txt.Text = query(0)
于 2010-03-08T17:52:47.177 回答
0

这对我有用。

Dim query = From row As dbDataSet.conformalRow
        In dbDataSet.Tables("conformal") _ 
        Where row.Cal.Length > 0 AndAlso tiCal_drop.Text = row.Cal _ 
            AndAlso row.Tran.Length > 0 AndAlso tiTrans_drop.Text = row.Tran _ 
            AndAlso row.barrel.Length > 0 _ 
        Select row.barrel 
于 2012-03-25T15:42:23.140 回答