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

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

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

我对qazxsw poi,qazxsw poi,qazxsw poi感到有点困惑。

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

.MoveLast

所以如果我没有错,那么带有记录源的部分应该是这样的:

.MoveFirst

但是后来,在代码中,我无法弄清楚如何编写等同于.Recordset 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等等...

vb.net vb6-migration
1个回答
2
投票

看起来你正在尝试将VB.Net等价物写入VB6 .MoveFirst等。

.Recordset

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

.Recordset

在这种情况下,Data19是一个DataTable,取代了VB6示例中的记录集。

.MoveLast

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

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

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

您将在下面看到转换为.Net时不需要此变量。您可以在VB6中使用它来了解要循环的记录数(以及何时停止)。在.Net中,您将使用.MoveLast来实现相同的目的。

.MoveFirst

对于您的示例,仅使用.MoveLast是因为您使用Dim row_count As Integer = Data19.Rows.Count 来获取记录计数。为了遍历记录集,您必须返回第一条记录。由于您不再需要在.Net中使用For Each.. Next,因此您也不需要使用.MoveFirst

.MoveNext

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

.MoveLast

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

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