Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. access autonincrement question

access autonincrement question

Scheduled Pinned Locked Moved C#
questioncsharpdatabaseannouncementlearning
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    theStorminMormon
    wrote on last edited by
    #1

    I have an MS Access table that I'm accessing from C# (via MSDE 2000). The primary key in the table is just an int column with identity set to "yes" and step set to '1'. So whenever I add a record Access automatically gives it a new primary key. Yay. That works fine, but what I'd really like is to be able to retrieve the key that Access generates at the time that I insert the new record. So right now my C# program just figures out the values for the non-primary keys and then sends an INSERT INTO command. Is there anyway to automatically retrieve the new ID that Access generates? The only idea that I have so far is to create an additional column in the table called "isNew" or something. When the user submits the new record, the SQL statement could set the "isNew" column for that record to the user name. Then I could immediately send a SELECT to retrieve the ID WHERE isNew = 'user name'. Of course, then I'd also have to send yet another UPDATE to SET isNew = ''. So it's kind of a cumbersome solution, but at least it will work. Anyone have anything better? The ends can never justify the means. It is the means that determine the ends.

    G 1 Reply Last reply
    0
    • T theStorminMormon

      I have an MS Access table that I'm accessing from C# (via MSDE 2000). The primary key in the table is just an int column with identity set to "yes" and step set to '1'. So whenever I add a record Access automatically gives it a new primary key. Yay. That works fine, but what I'd really like is to be able to retrieve the key that Access generates at the time that I insert the new record. So right now my C# program just figures out the values for the non-primary keys and then sends an INSERT INTO command. Is there anyway to automatically retrieve the new ID that Access generates? The only idea that I have so far is to create an additional column in the table called "isNew" or something. When the user submits the new record, the SQL statement could set the "isNew" column for that record to the user name. Then I could immediately send a SELECT to retrieve the ID WHERE isNew = 'user name'. Of course, then I'd also have to send yet another UPDATE to SET isNew = ''. So it's kind of a cumbersome solution, but at least it will work. Anyone have anything better? The ends can never justify the means. It is the means that determine the ends.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Get the most recently created id by running the query "select @@identidy" with the same database connection. --- b { font-weight: normal; }

      T 1 Reply Last reply
      0
      • G Guffa

        Get the most recently created id by running the query "select @@identidy" with the same database connection. --- b { font-weight: normal; }

        T Offline
        T Offline
        theStorminMormon
        wrote on last edited by
        #3

        Thanks! I think that will work well. My only concern is that if two users simultaneously add records to the table, there could be trouble. But if I run the select query within a line or two of running the insert into query the chances of this happening are very low, right? I would still like to have a full-proof method for future reference though. While I will only have a handful of users on this database at the same time, the program might conceivably need to be scaled up to over a 1,000 users. Any idea what might work in that case? The ends can never justify the means. It is the means that determine the ends.

        M G 2 Replies Last reply
        0
        • T theStorminMormon

          Thanks! I think that will work well. My only concern is that if two users simultaneously add records to the table, there could be trouble. But if I run the select query within a line or two of running the insert into query the chances of this happening are very low, right? I would still like to have a full-proof method for future reference though. While I will only have a handful of users on this database at the same time, the program might conceivably need to be scaled up to over a 1,000 users. Any idea what might work in that case? The ends can never justify the means. It is the means that determine the ends.

          M Offline
          M Offline
          miah alom
          wrote on last edited by
          #4

          Using identity column is not a good idea. There are many problems associated one being identity gap. I think you should implement your own increment procedure. Have a table with the follwoing schema CREATE TABLE dbo.Sequence ( TableName varchar(30) NOT NULL, NextVal int NOT NULL ) LOCK DATAROWS go GetSequence should get you the id CREATE PROCEDURE GetSequence ( @tbl varchar(30)) AS DECLARE @nextval int BEGIN BEGIN TRAN UPDATE Sequence SET NextVal = NextVal + 1 WHERE TableName = @tbl IF @@rowcount = 1 SELECT @nextval = NextVal FROM Sequence WHERE TableName = @tbl COMMIT TRAN SELECT @nextval END This will work even if there are 100 concurrent users

          1 Reply Last reply
          0
          • T theStorminMormon

            Thanks! I think that will work well. My only concern is that if two users simultaneously add records to the table, there could be trouble. But if I run the select query within a line or two of running the insert into query the chances of this happening are very low, right? I would still like to have a full-proof method for future reference though. While I will only have a handful of users on this database at the same time, the program might conceivably need to be scaled up to over a 1,000 users. Any idea what might work in that case? The ends can never justify the means. It is the means that determine the ends.

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            The @@identity method is absolutely safe. The value returned is the identity of the record last created within the same database session, e.g. with the same database connection. If two users create a record each at exactly the same time, they still have separate database sessions, so the id returned to each user is the id of the record that that specific user created. --- b { font-weight: normal; }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups