未为字符串和类型'Internalfield'定义VB.NET运算符'&'

时间:2019-07-11 12:40:39

标签: vb.net ms-access adodb arcobjects

背景

当前在2000年初开始在VBA中完成ArcMap插件的工作,并将其重构为VB.NET。该数据库是MS Access数据库。

对于VB.NET来说是相当新的东西,并且通常是编码方式。

错误

我面临的问题是我遇到了错误:

  

InvalidCastException未处理。

     

Microsoft.VisualBasic.dll中发生了'System.InvalidCastException'类型的未处理异常

     

其他信息:未为字符串“ UPDATE CustomerArea SET Area_m2 = 547”定义运算符'&',并键入'InternalField'。

代码

Dim nr
Dim area_polygon_excl_faktor As Double
Dim area_db

Do While Not AdoRS.BOF
   nr = AdoRS("nr")
   area_db = CDbl(AdoRS("area_m2").Value)

   Dim result As MsgBoxResult
   result = MsgBox(aAttrID(index) & "  " & area_db & "  " & area_polygon_exkl_faktor & "  Would you like to update the area?", vbYesNoCancel, "Update of area")

   If result = MsgBoxResult.Yes Then
      strSqlUpdateArea = "UPDATE CustomerArea SET Area_m2='" & area_polygon_excl_faktor & "' WHERE Nr= '" & nr

      AdoConn.Execute(strSqlUpdateArea)

   ElseIf result = MsgBoxResult.No Then

      MsgBox("You have decided not to update the area")

   End If

AdoRS是ADODB.Recordset。我面临的问题就在网上:

strSqlUpdateArea = "UPDATE CustomerArea SET Area_m2='" & area_polygon_excl_faktor & "' WHERE Nr= '" & nr

我尝试重写它几次,甚至在 nr = AdoRS(“ nr”),行中添加了Convert.toString,但是没有运气。我知道Access中的Nr是一个自动编号(LongInteger)。

2 个答案:

答案 0 :(得分:0)

  

旧的VBA代码看起来像这样...

然后不加引号,并强制使用小数点分隔符:

Dim format As System.Globalization.CultureInfo

format = System.Globalization.CultureInfo.InvariantCulture

strSqlUpdateArea = "UPDATE CustomerArea SET Area_m2 = " & area_polygon_excl_faktor.ToString(format) & " WHERE Nr = " & nr.ToString(format)

答案 1 :(得分:0)

解决方案是我必须将类型InternalField正确转换为字符串,并按正确的顺序执行步骤。

Dim sNr As String
Dim area_polygon_excl_faktor As Double
Dim area_db

Do While Not AdoRS.BOF
   sNr = Convert.ToString(AdoRS("nr").Value)
   area_db = CDbl(AdoRS("area_m2").Value)

   Dim result As MsgBoxResult
   result = MsgBox(aAttrID(index) & "  " & area_db & "  " & area_polygon_excl_faktor & "  Would you like to update the area?", vbYesNoCancel, "Update of area")

   If result = MsgBoxResult.Yes Then
      strSqlUpdateArea = "UPDATE CustomerArea Set Area_m2='" & area_polygon_excl_faktor & "' WHERE Nr=" & sNr

   AdoConn.Execute(strSqlUpdateArea)
   ElseIf result = MsgBoxResult.No Then
      MsgBox("You have decided not to update the area")
   End If