HighTechTalks DotNet Forums  

Generating GUID

Visual Studio.net (General) microsoft.public.vsnet.general


Discuss Generating GUID in the Visual Studio.net (General) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Ammar S. Mitoori
 
Posts: n/a

Default Generating GUID - 06-30-2009 , 02:25 AM






hi

i would like to give each record in my application a uniqe identifier how
can i generate it ? how to use system.guid to generate ?

Reply With Quote
  #2  
Old   
Hongye Sun [MSFT]
 
Posts: n/a

Default RE: Generating GUID - 06-30-2009 , 05:28 AM






Hi,

Thanks for your post.

What's your programming language? Here I use C# to demostrate guid
generation:

Guid g = Guid.NewGuid();
string guid = g.ToString();

How many records will be generated in your application? Although it is very
rare to happen that two generated guids are same. But if it has millions or
billions of generated records. It does have chances that two records has
same guid. Please take it into your consideration in your application.

Another way to keep that ID unique is to use incremental identifier, which
is widely used in database. To implement it by setting a integer or double
indentifier and increment it by 1 for every new record. This way has the
benifit that it saves memory and no duplication compared with guid.
However, it has limit of the length of its Type. For example, int's max
value is 2,147,483,647.

Please let me know if you need more information about it. Thanks.

Best Regards,
Hongye Sun (hongyes (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg (AT) microsoft (DOT) com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within?2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Reply With Quote
  #3  
Old   
Ammar S. Mitoori
 
Posts: n/a

Default RE: Generating GUID - 06-30-2009 , 02:40 PM



hi

i use VB.Net

so this incremental identifier will it be unique in all records in all
tables of an application ?
cause the incremental i know is unique in one table only ?

also how to use unique identifier in Vb

regards



"Ammar S. Mitoori" wrote:

Quote:
hi

i would like to give each record in my application a uniqe identifier how
can i generate it ? how to use system.guid to generate ?

Reply With Quote
  #4  
Old   
Hongye Sun [MSFT]
 
Posts: n/a

Default RE: Generating GUID - 06-30-2009 , 09:56 PM



Hi Ammar,

Thanks for your reply.

Here is the code sample to use GUID in VB.net

Dim g As Guid = Guid.NewGuid()
' Generate GUID string
Dim guidStr As String = g.ToString()
' Generate GUID byte array
Dim guidByte As Byte() = g.ToByteArray()

Are you using database in your application? If yes, to use incremental ID
in all records, usually, we use a table which is responsible to generate
unique id for all other records. The ID table only has one incremental ID
column. Every time a new record is created, we insert one item in ID table
and get its ID to assign it to the new record.

Thanks,
Hongye Sun (hongyes (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg (AT) microsoft (DOT) com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Reply With Quote
  #5  
Old   
Ammar S. Mitoori
 
Posts: n/a

Default RE: Generating GUID - 07-23-2009 , 06:50 AM



thanks for the replay

but how can i get the value of the last record in that ID table
can you suggest some VB.NET code please ?

Regards

""Hongye Sun [MSFT]"" wrote:

Quote:
Hi Ammar,

Thanks for your reply.

Here is the code sample to use GUID in VB.net

Dim g As Guid = Guid.NewGuid()
' Generate GUID string
Dim guidStr As String = g.ToString()
' Generate GUID byte array
Dim guidByte As Byte() = g.ToByteArray()

Are you using database in your application? If yes, to use incremental ID
in all records, usually, we use a table which is responsible to generate
unique id for all other records. The ID table only has one incremental ID
column. Every time a new record is created, we insert one item in ID table
and get its ID to assign it to the new record.

Thanks,
Hongye Sun (hongyes (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg (AT) microsoft (DOT) com.

This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #6  
Old   
Hongye Sun [MSFT]
 
Posts: n/a

Default RE: Generating GUID - 07-27-2009 , 11:33 AM



Hi Ammar,

In order to generate global ID from one table, you can:
1. Create one table GID with only one colume ID:

CREATE TABLE [dbo].[GID](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_GID] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

2. Write following function to insert and get MAX id from table:

Public Function NewSequenceId()
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim dr As SqlDataReader
Dim id As Long
myConnection = New SqlConnection("<Replace with your connection
string")
Try
myConnection.Open()
'opening the connection
myCommand = New SqlCommand("INSERT INTO GID DEFAULT VALUES",
myConnection)
myCommand.ExecuteNonQuery()

myCommand.CommandText = "SELECT MAX(ID) FROM GID"

'executing the command and assigning it to connection
dr = myCommand.ExecuteReader()
dr.Read()
id = dr.GetInt64(0)
Catch e As Exception
Finally
dr.Close()
myConnection.Close()
End Try

Return id
End Function

The return value is a long type value which is global unique.

I found a TSQL method which can generate GUID which is bigger than the last
GUID. This method avoid duplicate possiblities. Please read the link below:
http://msdn.microsoft.com/en-us/library/ms189786.aspx

Sincerely,
Hongye Sun (hongyes (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg (AT) microsoft (DOT) com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Reply With Quote
  #7  
Old   
Hongye Sun [MSFT]
 
Posts: n/a

Default RE: Generating GUID - 08-03-2009 , 12:21 AM



Hi Ammar,

Is this issue resolved? Please let me know what else I can help.

Sincerely,
Hongye Sun (hongyes (AT) online (DOT) microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg (AT) microsoft (DOT) com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.