Solved

C# primary key

Posted on 2016-10-04
9
60 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 28

Expert Comment

by:Pawan Kumar
ID: 41829212
Yes possible..

SQL Solution , try

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

Expert Comment

by:Pawan Kumar
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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
 

Author Comment

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

Accepted Solution

by:
Pawan Kumar 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 28

Expert Comment

by:Pawan Kumar
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 47

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

778 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