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. Unique numbers in SQL

Unique numbers in SQL

Scheduled Pinned Locked Moved C#
databasequestion
4 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
    totig
    wrote on last edited by
    #1

    I have been trying to use unique numbers, but this does not seem to work. The data wizard will always leave them out, and I cannot find a value similar to unique number. Anyway, so I have thought, maybe I should make my own. So I want to know the best way to do this. Do I, count the number of records, and add one (bearing in mind, data will never be deleted), or do I search through all records, and find the largest number in the column, and add 1 to this? If so for the last one, how would I do that? Would a while loop or a for loop be better? Lastly, in SQL, the column is set to uniqueNumber. Should I use bigInt?

    T 1 Reply Last reply
    0
    • T totig

      I have been trying to use unique numbers, but this does not seem to work. The data wizard will always leave them out, and I cannot find a value similar to unique number. Anyway, so I have thought, maybe I should make my own. So I want to know the best way to do this. Do I, count the number of records, and add one (bearing in mind, data will never be deleted), or do I search through all records, and find the largest number in the column, and add 1 to this? If so for the last one, how would I do that? Would a while loop or a for loop be better? Lastly, in SQL, the column is set to uniqueNumber. Should I use bigInt?

      T Offline
      T Offline
      totig
      wrote on last edited by
      #2

      Seems a bit odd replying to myself, but I found a solution, so this is for anyone who ever has a similar thing. This is if you want to create your own unique number. The code is: int test1 = 0; int test2 = 0; for (int i = 0; i <= yourDataSet.yourTableName.Count - 1; i++) {test1 = yourDataSet.yourTableName[i].theID; if (test2 <= test1) {test2 = test1 + 1; }} Sorry about how I compressed this. It counts the number the number of rows (in the format 1, 2, 3, etc, which is why you must - 1, else you will get an error). The last part just compares the numbers.

      L A 2 Replies Last reply
      0
      • T totig

        Seems a bit odd replying to myself, but I found a solution, so this is for anyone who ever has a similar thing. This is if you want to create your own unique number. The code is: int test1 = 0; int test2 = 0; for (int i = 0; i <= yourDataSet.yourTableName.Count - 1; i++) {test1 = yourDataSet.yourTableName[i].theID; if (test2 <= test1) {test2 = test1 + 1; }} Sorry about how I compressed this. It counts the number the number of rows (in the format 1, 2, 3, etc, which is why you must - 1, else you will get an error). The last part just compares the numbers.

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        Normally you would just set the column as read only and let SQL autoincrement it, or create a GUID.

        leppie::AllocCPArticle(Generic DFA State Machine for .NET);

        1 Reply Last reply
        0
        • T totig

          Seems a bit odd replying to myself, but I found a solution, so this is for anyone who ever has a similar thing. This is if you want to create your own unique number. The code is: int test1 = 0; int test2 = 0; for (int i = 0; i <= yourDataSet.yourTableName.Count - 1; i++) {test1 = yourDataSet.yourTableName[i].theID; if (test2 <= test1) {test2 = test1 + 1; }} Sorry about how I compressed this. It counts the number the number of rows (in the format 1, 2, 3, etc, which is why you must - 1, else you will get an error). The last part just compares the numbers.

          A Offline
          A Offline
          Adam Turner
          wrote on last edited by
          #4

          You could also setup in the DataSet that the column is auto-increment. So whenever you insert a new record, it will create a new ID for you. There is a problem with this though, as the ID always starts at 1 (or if populated from the Db, the last ID). So if your DB has 5 records in it with IDs of 1 though 5. And you retrieve ID 3 into your DataSet and add a new record, the ID will be 4. Now if you try to save it back to the database, it will error. Hope this helps

          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