473,809 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Consecutive GUID Generation in DotNet Framework

Current Framework 2.0/3.0.

...

In Sql Server, there is a way to generate consecutive guid's.
newsequentialid .

Is there a way to reproduce this type of consecutive guid's in the
framework.

I found some info about uuidgen , but that seems like a command line prompt
tool, not a class library.

I'd like to be able to throw an int value (N) at it, and it return N
consecutive guid's.
.........
Here is a test stored procedure I wrote to show you what I'm talking about.


IF EXISTS (SELECT * FROM sys.objects WHERE object_id =
OBJECT_ID(N'[dbo].[uspNewSequentia lUUIDCreateRang e]') AND type in (N'P',
N'PC'))

DROP PROCEDURE [dbo].[uspNewSequentia lUUIDCreateRang e]

GO

CREATE PROCEDURE [dbo].[uspNewSequentia lUUIDCreateRang e] (

@newUUIDCount int --return

)

AS

SET NOCOUNT ON

declare @t table ( dummyid int , entryid int identity(1,1) , uuid
uniqueidentifie r default newsequentialid () )

insert into @t ( dummyid ) select top (@newUUIDCount) 0 from dbo.sysobjects
so with (nolock)

select entryid , uuid from @t

SET NOCOUNT OFF

GO



/*

--START TEST

set nocount ON

Create Table #HolderTable (entryid int , uuid uniqueidentifie r )

declare @NewUUIDCount int

select @NewUUIDCount = 20

INSERT INTO #HolderTable EXEC dbo.uspNewSeque ntialUUIDCreate Range
@NewUUIDCount

select * from #HolderTable

DROP Table #HolderTable

--END TEST CODE

*/
Sep 5 '08 #1
16 2391
System.Guid.New Guid()

--
John Saunders | MVP - Connected System Developer
Sep 5 '08 #2
Just use whatever constructor best fit your needs :
http://msdn.microsoft.com/en-us/libr...guid.guid.aspx

--
Patrice

"sloan" <sl***@ipass.ne ta écrit dans le message de groupe de discussion :
uf************* *@TK2MSFTNGP05. phx.gbl...
Current Framework 2.0/3.0.

..

In Sql Server, there is a way to generate consecutive guid's.
newsequentialid .

Is there a way to reproduce this type of consecutive guid's in the
framework.

I found some info about uuidgen , but that seems like a command line
prompt tool, not a class library.

I'd like to be able to throw an int value (N) at it, and it return N
consecutive guid's.
........
Here is a test stored procedure I wrote to show you what I'm talking
about.


IF EXISTS (SELECT * FROM sys.objects WHERE object_id =
OBJECT_ID(N'[dbo].[uspNewSequentia lUUIDCreateRang e]') AND type in (N'P',
N'PC'))

DROP PROCEDURE [dbo].[uspNewSequentia lUUIDCreateRang e]

GO

CREATE PROCEDURE [dbo].[uspNewSequentia lUUIDCreateRang e] (

@newUUIDCount int --return

)

AS

SET NOCOUNT ON

declare @t table ( dummyid int , entryid int identity(1,1) , uuid
uniqueidentifie r default newsequentialid () )

insert into @t ( dummyid ) select top (@newUUIDCount) 0 from
dbo.sysobjects so with (nolock)

select entryid , uuid from @t

SET NOCOUNT OFF

GO



/*

--START TEST

set nocount ON

Create Table #HolderTable (entryid int , uuid uniqueidentifie r )

declare @NewUUIDCount int

select @NewUUIDCount = 20

INSERT INTO #HolderTable EXEC dbo.uspNewSeque ntialUUIDCreate Range
@NewUUIDCount

select * from #HolderTable

DROP Table #HolderTable

--END TEST CODE

*/

Sep 5 '08 #3
I don't see a way to do this with the System.Guid class. newsequentialid was
introduced to provide some optimization in SQL, but that was not part of the
intent of GUIDs. GUIDs are somewhat random, and newsequentialid does away
with the randomness.
"sloan" wrote:
Current Framework 2.0/3.0.

...

In Sql Server, there is a way to generate consecutive guid's.
newsequentialid .

Is there a way to reproduce this type of consecutive guid's in the
framework.

I found some info about uuidgen , but that seems like a command line prompt
tool, not a class library.

I'd like to be able to throw an int value (N) at it, and it return N
consecutive guid's.
.........
Here is a test stored procedure I wrote to show you what I'm talking about.


IF EXISTS (SELECT * FROM sys.objects WHERE object_id =
OBJECT_ID(N'[dbo].[uspNewSequentia lUUIDCreateRang e]') AND type in (N'P',
N'PC'))

DROP PROCEDURE [dbo].[uspNewSequentia lUUIDCreateRang e]

GO

CREATE PROCEDURE [dbo].[uspNewSequentia lUUIDCreateRang e] (

@newUUIDCount int --return

)

AS

SET NOCOUNT ON

declare @t table ( dummyid int , entryid int identity(1,1) , uuid
uniqueidentifie r default newsequentialid () )

insert into @t ( dummyid ) select top (@newUUIDCount) 0 from dbo.sysobjects
so with (nolock)

select entryid , uuid from @t

SET NOCOUNT OFF

GO



/*

--START TEST

set nocount ON

Create Table #HolderTable (entryid int , uuid uniqueidentifie r )

declare @NewUUIDCount int

select @NewUUIDCount = 20

INSERT INTO #HolderTable EXEC dbo.uspNewSeque ntialUUIDCreate Range
@NewUUIDCount

select * from #HolderTable

DROP Table #HolderTable

--END TEST CODE

*/
Sep 5 '08 #4
Mike,

Thanks for actually reading my post.

Yeah, I don't see a way to do it with System.Guid. ( I was already aware of
that class' existence ).

I may use my usp and push some sequential guid's up to the business layer,
since I'll already be in the database.
Or delay their creation until I'm pushing some data back into the tsql.

Oh well, I thought I'd ask.


"Family Tree Mike" <Fa************ @discussions.mi crosoft.comwrot e in
message news:76******** *************** ***********@mic rosoft.com...
>I don't see a way to do this with the System.Guid class. newsequentialid
was
introduced to provide some optimization in SQL, but that was not part of
the
intent of GUIDs. GUIDs are somewhat random, and newsequentialid does away
with the randomness.
"sloan" wrote:
>Current Framework 2.0/3.0.

...

In Sql Server, there is a way to generate consecutive guid's.
newsequentiali d.

Is there a way to reproduce this type of consecutive guid's in the
framework.

I found some info about uuidgen , but that seems like a command line
prompt
tool, not a class library.

I'd like to be able to throw an int value (N) at it, and it return N
consecutive guid's.
.........
Here is a test stored procedure I wrote to show you what I'm talking
about.


IF EXISTS (SELECT * FROM sys.objects WHERE object_id =
OBJECT_ID(N'[dbo].[uspNewSequentia lUUIDCreateRang e]') AND type in (N'P',
N'PC'))

DROP PROCEDURE [dbo].[uspNewSequentia lUUIDCreateRang e]

GO

CREATE PROCEDURE [dbo].[uspNewSequentia lUUIDCreateRang e] (

@newUUIDCoun t int --return

)

AS

SET NOCOUNT ON

declare @t table ( dummyid int , entryid int identity(1,1) , uuid
uniqueidentifi er default newsequentialid () )

insert into @t ( dummyid ) select top (@newUUIDCount) 0 from
dbo.sysobjec ts
so with (nolock)

select entryid , uuid from @t

SET NOCOUNT OFF

GO



/*

--START TEST

set nocount ON

Create Table #HolderTable (entryid int , uuid uniqueidentifie r )

declare @NewUUIDCount int

select @NewUUIDCount = 20

INSERT INTO #HolderTable EXEC dbo.uspNewSeque ntialUUIDCreate Range
@NewUUIDCoun t

select * from #HolderTable

DROP Table #HolderTable

--END TEST CODE

*/

Sep 5 '08 #5
I don't understand - why do you need them to be sequential? Is it not enough
that each one is unique?
--
John Saunders | MVP - Connected System Developer

Sep 5 '08 #6
"John Saunders" <no@dont.do.tha t.comwrote in message
news:uv******** ******@TK2MSFTN GP02.phx.gbl...
>I don't understand - why do you need them to be sequential? Is it not
enough that each one is unique?
If you are using them as a unique or primary key in a database such a SQL
server their highly random nature makes them less than desirable. Hence SQL
server provides the newsequentialid function which generates a form of Guid
which more predictable and when compared each new id is than the previous.
This increases slightly the risk of collision but since the scope of use is
so narrow its beyond worrying about and yet provides a good basis for a
clustered key.

--
Anthony Jones - MVP ASP/ASP.NET

Sep 5 '08 #7
There are really good reasons to use a unique, sequential PK--most of the
reasons have to do with how the index pages are managed and index
fragmentation. While the NEWSEQUENTIALID is _UNIQUE_ it is not globally
unique unless the system has a NIC card (it uses the MAC address to generate
the uniqueness) so there is NO problem with concurrency in any case.

I quote from the doc: "Each GUID generated by using NEWSEQUENTIALID () is
unique on that computer. GUIDs generated by using NEWSEQUENTIALID () are
unique across multiple computers only if the source computer has a network
card."

hth

--
_______________ _______________ _______________ _______________ ______________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
_______________ _______________ _______________ _______________ _______________ _______________ __

"Anthony Jones" <An***********@ yadayadayada.co mwrote in message
news:eY******** ******@TK2MSFTN GP04.phx.gbl...
"John Saunders" <no@dont.do.tha t.comwrote in message
news:uv******** ******@TK2MSFTN GP02.phx.gbl...
>>I don't understand - why do you need them to be sequential? Is it not
enough that each one is unique?

If you are using them as a unique or primary key in a database such a SQL
server their highly random nature makes them less than desirable. Hence
SQL server provides the newsequentialid function which generates a form of
Guid which more predictable and when compared each new id is than the
previous. This increases slightly the risk of collision but since the
scope of use is so narrow its beyond worrying about and yet provides a
good basis for a clustered key.

--
Anthony Jones - MVP ASP/ASP.NET
Sep 5 '08 #8

Ding Ding Ding Ding Ding!

That's for explaining it, so I didn't have to. I'm worn out this week.

............... ......

And sometimes we build our relationships OUTSIDE of the database, and shoot
them in via xml.
And we use a surrogate key......and with set based inserts......a
consecutive guid would be beneficial.

We have been relying on the newsequentialid (as the default value for the
uniqueidentifie r primary key), but I was going to experiment with creating
the surrogate key outside of the database and pushing it in instead.

..............

This was experimentation stuff. And trying to figure out if having them
generated outside of the db provided any benefit.

One of the biggest wait stats we have is
PAGELATCH_EX.

I'm not a dba, I'm not trying to experiment to see if the "outside
sequential guid" creation might help.

Maybe someone can comment on the

PAGELATCH_EX ... and if I'm barking up the wrong tree or not.


PAGELATCH_EX

True
Occurs when a task is waiting for a latch for a buffer that is not in
an I/O request. The latch request is in Exclusive mode.

Contention can be caused by issues other than IO or memory
performance, for example, heavy concurrent inserts into the same index range
can cause this kind of contention. If many inserts must be added on the same
page, they are serialized using the latch. Lots of inserts into the same
range can also cause page splits in the index which holds onto the latch
while allocating a new page (this can take time). Any read accesses to the
same range as the inserts would also conflict on the latches. The solution
in these cases is to distribute the inserts using a more appropriate index.


"Anthony Jones" <An***********@ yadayadayada.co mwrote in message
news:eY******** ******@TK2MSFTN GP04.phx.gbl...
"John Saunders" <no@dont.do.tha t.comwrote in message
news:uv******** ******@TK2MSFTN GP02.phx.gbl...
>>I don't understand - why do you need them to be sequential? Is it not
enough that each one is unique?

If you are using them as a unique or primary key in a database such a SQL
server their highly random nature makes them less than desirable. Hence
SQL server provides the newsequentialid function which generates a form of
Guid which more predictable and when compared each new id is than the
previous. This increases slightly the risk of collision but since the
scope of use is so narrow its beyond worrying about and yet provides a
good basis for a clustered key.

--
Anthony Jones - MVP ASP/ASP.NET

Sep 5 '08 #9
If you are using them as a unique or primary key in a database such a SQL
server their highly random nature makes them less than desirable. Hence
SQL server provides the newsequentialid function which generates a form of
Guid
Why/when would that be preferable over just using an incremental numeric ID
field?

-Darrel

Sep 5 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5540
by: Scott Meddows | last post by:
In VB.net you can dim aGUID as System.GUID aGuid = Guid.NewGuid() >-----Original Message----- >Hi, >I need to know how to generate a GUID programmatically. I >basically want to use the guids as a property of another
8
9064
by: Tamir Khason | last post by:
I want to build Guid Array so doing: Guid data = new Guid; .... and recieve an error "; expected" - What's the hell? TNX -- Tamir Khason You want dot.NET? Just ask: "Please, www.dotnet.us "
10
3468
by: www.dir | last post by:
Hi, The following is from MSDN. "A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated." My application will crush if I have two or more number that are identical, so I need to know do I need to worry about this"
3
1968
by: Marty McDonald | last post by:
Using Visual Studio.Net... I have two classes, one derives from the other. My web service accepts the base class as input, it returns the derived class as the return value. When I set a web reference to that web service, the web service proxy is incorrect! It creates its own version of the base class fine, but when it creates its own version of the derived class, it shows it as simply deriving from the base, but its own members are not...
6
7203
by: SevDer | last post by:
Is there a way to test guid string? I want to do it without try catch block to save on performance. Thanks in advance. -- SevDer
0
1066
by: Tim Golden | last post by:
Robert Rawlins wrote: Not only is the answer, Yes: http://docs.python.org/lib/module-uuid.html but it's just featured as Doug Hellmann's module of the Week: http://blog.doughellmann.com/2008/07/pymotw-uuid.html
16
1752
by: sloan | last post by:
Current Framework 2.0/3.0. ... In Sql Server, there is a way to generate consecutive guid's. newsequentialid. Is there a way to reproduce this type of consecutive guid's in the framework.
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.