如何使用recordset movelast

时间:2012-07-07 06:17:15

标签: vb.net visual-studio-2010 sql-server-2008 ado.net

我需要帮助我可以使用旧的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

1 个答案:

答案 0 :(得分: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