【问题标题】:Migrating VB6 to VB.net recordsets and datatables将 VB6 迁移到 VB.net 记录集和数据表
【发布时间】:2019-02-14 15:01:34
【问题描述】:

我在 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 

我对@9​​87654322@、.MoveFirst.Recordset有点困惑。

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

  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 等的等价物...

【问题讨论】:

  • 看大局——vb6的大块是做什么的?只需在 vb.net 中开发,不要卡在逐行转换代码上。如果您不知道它的作用并尝试逐行转换,那么您可能无论如何都不会成功!
  • 如果您有很多代码,请考虑参考 ADO - 您的代码将像以前一样工作。

标签: vb.net vb6-migration


【解决方案1】:

您似乎正在尝试将 VB.Net 等价物编写为 VB6 .MoveLast, .MoveFirst, .Recordset, .MoveNext 等。

.记录集

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

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 更新语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多