Solved

C# primary key

Posted on 2016-10-04
9
53 Views
Last Modified: 2016-10-05
I would like to create unique identifier like below.

1. h111111

the format is
1. Must start "h"
2. with 5 numbers only

the 5 numbers can be random, but once it is created, it can't be duplicated.

is it possible?
0
Comment
Question by:ITsolutionWizard
9 Comments
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
ID: 41829212
Yes possible..

SQL Solution , try

SELECT CONCAT( 'h' , RIGHT(RAND(),5) )
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
ID: 41829213
One more option for < SQL Server 2012

SELECT 'h' + CAST(CONVERT(NUMERIC(12,0),RAND() * 99999) + 10000 AS VARCHAR(7))
0
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 41829228
By Unique constraint to the column, you can achieve this.
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 41829231
Since you have to generate next ID in a custom format, better go for sequence approach to make sure no duplicates.
Try any of the explained approach here
http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:ITsolutionWizard
ID: 41829239
Can we use it without sql?
0
 
LVL 17

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
ID: 41829278
Try below options..

Random rnd = new Random();
        string str1 = "H" + rnd.Next().ToString().Substring(0, 5);


        char[] ch = Guid.NewGuid().ToString().Where(x => char.IsNumber(Convert.ToChar(x))).Take(5).ToArray();
        string str2 = "H" + (new string(ch));

--

Open in new window


Enjoy!!
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
ID: 41829344
Yes you can also create unique constraints..Approach below.

DataTable at ;

UniqueConstraint aUnique =  new UniqueConstraint(
new DataColumn[] { at.Columns["ID"], at.Columns["Name"] });

at.Constraints.Add(aUnique);
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41829376
the 5 numbers can be random, but once it is created, it can't be duplicated.
Choose one of the above solutions to implement the generation of the values but to assure that's no duplicates you'll need to create an unique index on the field:
CREATE UNIQUE INDEX IndexName ON TableName (ColName)

Open in new window


or create a Primary Key if you don't have one in the table:
ALTER TABLE TableName  
ADD CONSTRAINT PK_TableName_ColName PRIMARY KEY (Colname)

Open in new window

1
 

Author Closing Comment

by:ITsolutionWizard
ID: 41831144
This is the best solution. Without using SQL is perfect. Good Job
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now