Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Basic sql c# question

  • 24-02-2009 12:06PM
    #1
    Closed Accounts Posts: 532 ✭✭✭


    I have two tables, one called Users that has a primary column called UserID and another table called Info that has a column with the same name (UserID). The Info->UserID is a foreign key of Users-UserID.

    When I add a new user I want to take the UserID number that is generated automatically for Users-UserID and use it for the Info->UserID. So basically I need to find the highest UserID in the Users table and put it in Info->UserID.

    How can I do this in c#, that is, create and exectue a query that returns me the highest value in Users->UserID?


Comments

  • Registered Users, Registered Users 2 Posts: 2,774 ✭✭✭LowOdour


    Are you using procs?
    OK, in your sql you want to return the new Id
    @IDENTITY) as UserId'
    This returns the new identity created.

    In your c# code, you return the new Id by an ExecuteScalar command and it can be added to a variable for use on the Info table. Thats an overview, should give you the basics to figure out the rest.

    As a side-note, do you need an Info table? For a User (which is unique I pressume), do you want to hold many number of rows of info against him/her? If its just going to be one info record for one user, i would suggest just having the one table!



Advertisement