Unique numbers in SQL
-
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?
-
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?
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.
-
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.
Normally you would just set the column as read only and let SQL autoincrement it, or create a GUID.
-
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.
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