+ -
当前位置:首页 → 问答吧 → 高分求教:ado迁移到ado.net

高分求教:ado迁移到ado.net

时间:2011-10-22

来源:互联网

现有一Access数据库应用程序是基于vb6.0用ado进行数据访问的,其中一主要的数据结构如下:

表Score

主键 ID
考试号 TestID 整型
学生编号 LearnNo 文本(10)
语文成绩 Chinese 数值型
语文排名 ChineseSort 整型
数学成绩 Maths 数值型
数学排名 MathsSort 整型
英语成绩 English 数值型
英语排名 EnglsihSort 整型
三科总分 Total3 数值型
三科排名 Total3Sort 整型

在取得语文、数学、英语三科成绩后,要求计算出三科总分及语文、数学、英语、三科总分的排名。

vb6的实现方法如下:

VB code

Private Sub CountTotal3(iTestID As Integer)
  Dim sql As String, rsScore As New ADODB.Recordset, dTotal3 As Double
  
  Me.Caption = "正在统计三科总分......"
  DoEvents
  sql = "Select * From Score Where TestID=" & CStr(iTestID)
  rsScore.CursorLocation = adUseClient
  rsScore.Open sql, cnHMZX, adOpenStatic, adLockOptimistic, adCmdText
  cnHMZX.BeginTrans
    rsScore.MoveLast
    rsScore.MoveFirst
    With rsScore
      Do While Not .EOF
        dTotal3 = 0
        If .Fields("Chinese") > 0 And .Fields("Maths") > 0 And .Fields("English") > 0 Then
          dTotal3 = .Fields("Chinese") + .Fields("Maths") + .Fields("English")
        End If
        .Fields("Total3") = dTotal3
        .Update
        .MoveNext
      Loop
    End With
  cnHMZX.CommitTrans
  rsScore.Close
  
  Set rsScore = Nothing
End Sub

Private Sub CountSort(iTestID As Integer, strSubject As String)
  Dim k As Integer, n As Integer
  Dim dScore As Double, sql As String, rsScore As New ADODB.Recordset
  
  Me.Caption = "正在统计" & strSubject & "的排名......"
  DoEvents
  
  sql = "Select * From Score Where TestID=" & CStr(iTestID)
  sql = sql & " Order By " & strSubject & " DESC"
  With rsScore
    .Open sql, cnHMZX, adOpenStatic, adLockOptimistic, adCmdText
    k = 0
    n = 0
    dScore = 0
    Do While Not .EOF
      If .Fields(strSubject) <> dScore Then
        k = k + 1 + n
        n = 0
        dScore = .Fields(strSubject)
      Else
        n = n + 1
      End If
      .Fields(strSubject & "Sort") = k
      .Update
      .MoveNext
    Loop
    .Close
  End With
  
  Set rsScore = Nothing
End Sub



其中cnHMZX是一个全局的变量,程序一运行就与数据建立连接。

请教,将上面的程序迁移到vb.net 2008使用ado.net,应该如何解决?

作者: vicsue   发布时间: 2011-10-22

VB数据库访问和.net数据库访问的的方法完全不一样

作者: worldy   发布时间: 2011-10-22