Results 1 to 3 of 3

Thread: SQL help pls

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    9

    Question SQL help pls

    Hi there,

    I recently started using VB.NET and doing my first VB/SQL project.

    I have very basic question.

    In VB 6 I used to get the last record result through below SQL (OleDB)

    Dim LastRecord as Integer
    adoPrimaryRS1.Open "Select * from BUY ORDER BY ID ASC", dB4, adOpenStatic, adLockOptimistic
    adoPrimaryRS1.MoveLast
    LastRecord = adoPrimaryRS1.Fields!Id

    and now I am using SQL Server client, could you please tell me how to achieve above. I am using the below code.


    Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 1 ID FROM dbo.Accounts ORDER BY ID DESC", CON)
    Dim DA As New SqlDataAdapter(cmd)
    Dim DT As New DataTable

    Many thanks in advance.

    Kh

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SQL help pls

    Why is it that I already replied to this exact same question in a different forum under a different account?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: SQL help pls

    Quote Originally Posted by jmcilhinney View Post
    Why is it that I already replied to this exact same question in a different forum under a different account?
    This not withstanding: What's so hard about to read the official documentation?
    I have no clue about MS SQL, but it took me 2 minutes to find it
    https://learn.microsoft.com/en-us/sq...l-server-ver16
    Code:
    SELECT IDENT_CURRENT ('dbo.Accounts') AS Current_Identity;
    Nevermind, that i've never understood the need to know the last inserted Identity.
    surrogate ID's should always be opaque to the user.
    The only situation you need them, is inserting records into a detail-table where you'd need them for the Foreign key,
    and then you just pass it through
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width