Click here to Skip to main content
12,395,277 members (66,056 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hi All,

I need some help from u people.My senario like this

I am inserting multiple records at the same time users are hitting the same time. But here i found Running number from front end(Get the max number from database and adding 1 to that number) and adding this number in each and every row and forming insert query.


So before executing insert query if others are also getting the same number so how to handle this one.

Thanks & Regards,
Posted 13-Jul-10 22:58pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Just add an integer ID field to the table that auto-increments.
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Yes, as John says let you database generate this number, if you need to use the number in the front end then just have your procedure return it after the insert has completed.

Something like...

CREATE PROCEDURE  example_procedure
 
	(
		@SomeVariable	INT,
		@AnoherVariable	INT,
		@UniqueFieldID	INT OUTPUT
 
	)
 
AS
 
SET NOCOUNT ON
 
INSERT INTO
	TheTable (Field1, Field2)
VALUES
	(@SomeVariable, @AnoherVariable)
 
--Return the new identity
SET @UniqueFieldID  =  SCOPE_IDENTITY()
 
-- Return any error codes \ Reset the NOCOUNT property.
RETURN @@ERROR
SET NOCOUNT OFF
GO
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

You can either turn the auto number on or you can also generate RANDOM NUMBER everytime as a primary key which will be diffrent for all the users.

No need to fetch the last record and add 1 in its id, instead just insert random number for all records which will be diffrent for all the users even if it wil be having entry in DB at same time.

(Random depends on the algorithm that you write)

Ashutosh Jain.
  Permalink  
Comments
aspdotnetdev 14-Jul-10 14:39pm
   
I recommend the IDENTITY field technique, but if somebody were taking the random number route, using a GUID would probably be the easiest way.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 14 Jul 2010
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100