Solved

C# primary key

Posted on 2016-10-04
9
64 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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 48

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

856 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