将 VB6 迁移到 VB.net 记录集和数据表

Migrating VB6 to VB.net recordsets and datatables

我有一部分 VB6 代码正在尝试转换为 VB.net。特别是记录集。

这是 VB6 代码的一部分:

Data19.RecordSource = "select id from headers where type=12"
Data19.Refresh
If Data19.Recordset.RecordCount > 0 Then
Data6.RecordSource = "select sum(left * right) from footers where type=12"
Data6.Refresh
ss = Format(Data6.Recordset.Fields(0), "0.00")
Data19.Recordset.MoveLast
a = Data19.Recordset.RecordCount - 1
Data19.Recordset.MoveFirst
For i = 0 To a
If i > 0 Then Data19.Recordset.MoveNext
   Data22.RecordSource = "select * from documents where type=12"
   Data19.Recordset.Fields(0)
   Data22.Refresh
   With Data22.Recordset
      If .RecordCount > 0 Then
      .MoveLast
      b = .RecordCount - 1
      .MoveFirst
        For j = 0 To b
          If j > 0 Then .MoveNext
              If .Fields("link1") < ra And .Fields("code") <> "00" Then
              .Edit
              .Fields("link1") = ra
              .Fields("pc") = Format(.Fields("dpc") * (100 - ra) / 100, "0.00")
              .Fields("total") = Format(.Fields("amount") * .Fields("dpc") * (100 - ra) / 100, "0.00")
              .Update
              End If
        Next
      End If
   End With
  Next
 End If 

我对.MoveLast.MoveFirst.Recordset有点困惑。

在我的 VB.net 代码中,我一直在使用此函数从数据库中获取我想要的 table:

  Public Function returnTable(ByVal queryString As String)
    Dim query1 As String = queryString
    'Console.WriteLine(query1)
    Dim table As New DataTable
    Using connection As New MySqlConnection(connection)
        Using adapter As New MySqlDataAdapter(query1, connection)
            Dim cmb As New MySqlCommandBuilder(adapter)
            table.Clear()
            adapter.Fill(table)
            Return table
        End Using
    End Using
End Function

如果我没记错的话,记录源部分应该是这样的:

Dim data19 as new datatable
Data19 = returnTable("select id from headers where type=12")

但后来,在代码中,我不知道如何编写 .MoveLast.MoveFirst.Recordset.MoveNext 等的等价物。 .

看起来您正在尝试编写 VB.Net 等同于 VB6 .MoveLast, .MoveFirst, .Recordset, .MoveNext 等的

.记录集

您已经非常接近您创建的 returnTable 函数的解决方案了。数据表只是 DataRow 对象的集合,这与 VB6 中的记录集对象有点不同。您使用 .Net 函数创建了一个数据表:

Dim data19 as new datatable
Data19 = returnTable("select id from headers where type=12")

在本例中,Data19 是一个 DataTable,它取代了 VB6 示例中的记录集。

.MoveLast

在您的 VB6 示例中,使用 .MoveLast 的原因是公开记录集中的记录数。在移动到最后一条记录之前,记录数是未知的。 移动到最后一条记录后,您可以将计数加载到变量中。

使用 ADO.Net,您不需要使用 .MoveLast 来获取计数。您可以像这样简单地获取行数:

Dim row_count As Integer = Data19.Rows.Count

您将在下面看到转换为 .Net 时不需要此变量。您可以在 VB6 中使用它来了解要循环多少条记录(以及何时停止)。在 .Net 中,您将使用 For Each.. Next 来达到同样的目的。

.MoveFirst

对于您的示例,使用 .MoveFirst 只是因为您使用 .MoveLast 来获取记录数。为了遍历记录集,您必须返回到第一条记录。 由于您不再需要在 .Net 中使用 .MoveLast,因此您也不需要使用 .MoveFirst.

.MoveNext

在您的 VB6 示例中,.MoveNext 用于遍历记录集并对每一行执行一些操作。要遍历您创建的 DataTable,您可以这样做:

Dim my_row as DataRow
For Each my_row in Data19.Rows
    If my_row("link1") < ra And my_row("code") <> "00" Then
        .. do some actions
    End If
Next

这将以类似的方式遍历记录集。需要考虑的一件事是,当您使用 DataTable 时,您正在使用一个断开连接的记录集。当您到达 VB6 过程的 .Edit.Update 部分时,您可能需要使用参数化查询来对任何记录执行实际更新。这将使用命令对象和 .ExecuteNonQuery() 方法来执行 SQL 更新语句。