0

我需要代码帮助,我可以使用旧的 rs.movelast 到 vb.net 2010 .. 任何简单的方法来查询我的记录,自动选择最后一个..

这是我的连接示例,我只是以任何形式调用它..///

Public Function ExecuteSQLQuery(ByVal SQLQuery As String) As DataTable
    Try
        Dim sqlCon As New OleDbConnection(CnString)
        Dim sqlDA As New OleDbDataAdapter(SQLQuery, sqlCon)
        Dim sqlCB As New OleDbCommandBuilder(sqlDA)
        sqlDT.Reset() ' refresh 
        sqlDA.Fill(sqlDT)
    Catch ex As Exception
        MsgBox("Error : " & ex.Message)
    End Try
    Return sqlDT
End Function
4

1 回答 1

0

sqlDT.rows(sqlDT.rows.count-1) 将是 DataTable sqlDT 的最后一条记录。sqlDT.rows.count-1 将返回填充表中行的最后一个索引。希望它会帮助你。谢谢

Imports System.Data.OleDb

Public Class Form1

Public CnString As String = "Provider=SQLOLEDB;Data Source=HP-PC\SQLEXPRESS;Persist Security Info=True;Password=sa;User ID=sa;Initial Catalog=Accounts"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim ssql As String = "Select * from TBL_Access"
    Dim dt As DataTable

    dt = ExecuteSQLQuery(ssql)
    TextBox1.Text = dt.Rows(dt.Rows.Count - 1)(0) 'Value of First Column of Last Row of DataTable dt
    TextBox2.Text = dt.Rows(dt.Rows.Count - 1)(1) 'Value of Second Column of Last Row of DataTable dt
End Sub

Public Function ExecuteSQLQuery(ByVal SQLQuery As String) As DataTable
    Try

        Dim sqlCon As New OleDbConnection(CnString)
        Dim sqlDA As New OleDbDataAdapter(SQLQuery, sqlCon)
        Dim sqlCB As New OleDbCommandBuilder(sqlDA)
        Dim sqlDT As New DataTable
        sqlDT.Reset() ' refresh  
        sqlDA.Fill(sqlDT)
        Return sqlDT
    Catch ex As Exception
        MsgBox("Error : " & ex.Message)
        Return Nothing

    End Try
End Function



End Class
于 2012-07-07T06:37:00.873 回答