Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i m using VB.net 2008 and Sql db as backend
on this software i m trying to generate automatically number as unique number or ID
can you help me on doing this
Posted
Comments

Edit your DB in SSMS, and add an ID column to your table: Set it as "int", and scroll the Column Properties window down to "Identity Specification".
Click the [+] to expand the property, and double click "(Is Identity)" to change it from "No" to "Yes".
Save your table.

SQL server will now provide a new ID value every time you save a row and will ensure that it is unique.
Do note that you cannot rely on the current value as an indicator of what the next value will be: if you do try you will work OK for testing, but the whole thing will collapse in production when you have more than one user. Only ever try to use an ID value after the row has been inserted.
 
Share this answer
 
Comments
CPallini 17-Dec-13 3:29am    
5.
[no name] 17-Dec-13 9:30am    
Please do also pay attention to the solution of petervanekeren. His solution is NOT THAT BAD!
OriginalGriff 17-Dec-13 9:38am    
Why are you shouting at me?
Do you think I down voted his solution?
Look at the points spread: 1 vote of 1, one vote of 5. for an average of 3 - which means that the upvote and downvote were done by people with a similar authority reputation.
My rep points mean that when I up or down vote something, it has a large impact: if it had been my down vote than it would have taken someone with a very similar authority to mine to balance it that closely, or several lower rep people.

Assuming the upvote was yours (as seems likely) you should be shouting at someone with a similar rep to your own...probably the OP in this case.

(This is why I rarely down-vote: I have too big an impact, and do too much damage to peoples reputation. And when I do, I say why - so that they know what not to do next time!)

[edit]Typos - OriginalGriff[/edit]
[no name] 17-Dec-13 11:46am    
Sorry it was in no way against you. I placed the comment here that there is a chance that the comment is noticed.
OriginalGriff 17-Dec-13 12:08pm    
It's OK. I'm surprisingly difficult to upset, even by accident! :laugh:
If you want a unique number that's globally unique you can use the NEWID() function in SQL.

SQL
SELECT NEWID()


will give you a GUID that looks like C7E987A9-958C-4A4C-95EC-E2AAEA6E0C08
 
Share this answer
 
Comments
[no name] 17-Dec-13 8:47am    
Why this answer has been downvoted? GUID is maybe overkill, but used widely in windows!

And if all COM objects can be based on GUID (because it is so unique.....) why not a DB key????

And by the way all who have a little bit an idea about balanced index trees, GUID is better in this view.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900