Solved

How to use GUIDs in C# code

Posted on 2013-01-18
6
350 Views
Last Modified: 2013-02-15
How do you use GUIDs in code for C#.  I know that for C# and running stored procedures against a SQL database INTs are used for IDs, strings are used for VARCHARs, etc....but what about GUIDs?  How do you run queries, stored procedures or data entries via code with C# against a GUID?  Thanks!
0
Comment
Question by:VBBRett
6 Comments
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 84 total points
ID: 38794606
0
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 84 total points
ID: 38794607
You use a guid like any other time e.g. datetime. Plenty of people use guids as the primary key in a database.

http://bytes.com/topic/c-sharp/answers/508614-retrieving-guid-datatable

http://stackoverflow.com/questions/904021/how-to-use-guids-in-c
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 83 total points
ID: 38794609
A GUID is a "Globally Unique IDentifier." It is a pseud0-random string of characters that you can use for a unique key. While it is highly unlikely that you would ever generate two identical GUIDs in your lifetime, it is theoretically possible--but highly improbable.

As far as running queries, SQL Server has the "uniqueidentifier" data type for columns. To pass a GUID to your command objects, you could do something like:

using (SqlCommand cmd = new SqlCommand("query", connection))
{
    cmd.Parameters.Add("@paramName", Guid.Parse("{25892e17-80f6-415f-9c65-7395632f0223}"));
}

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 20

Assisted Solution

by:Russ Suter
Russ Suter earned 83 total points
ID: 38794612
In SQL a Guid is known as a UNIQUEIDENTIFIER. They can easily be used for identity instead of INT. I do it all the time. It's especially useful when you need an identifier that cannot be easily guessed. The .NET framework can translate Guid to string and back again with ease. Just sent the Guid value to your SQL query as a string and SQL should handle it just fine. When reading the value back just use the Guid constructor overload that accepts a string as the argument.
0
 
LVL 6

Assisted Solution

by:esolve
esolve earned 83 total points
ID: 38797879
In order to declare and create a new Guid in C# you do this.

string sGuid = string.empty
Guid gGuid = System.Guid.NewGuid();
sGuid = gGuid.ToString();

you can then use this string and save it to the database also use this string to retrieve data from the database.

update table set myGuid = sGuid
select * from table where myGuid = sGuid
0
 
LVL 10

Accepted Solution

by:
Monica P earned 83 total points
ID: 38799962
SampleCode to use GUID

    Dim UserGuid As Guid, Ctr As Integer, Str As String

    Cmd.CommandText = "Select Name, PKID From Users Where Name = '" & UserID & "'"
    dr = Cmd.ExecuteReader
    dr.Read()
    UserGuid = dr.GetGuid(1) 'this shows the Guid
    dr.Close()
    Str = UserGuid.ToString
    Cmd.CommandText = "Insert Into test(TranAmt, UserGuid) Values (250, '" & GuidStr & "')"
    Ctr = Cmd.ExecuteNonQuery

More info on GetGuid
***********
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getguid.aspx
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to check to see if datatable has headers or not 2 27
Cant save 3D 4 19
Hey!!!! 1 20
Create a WebAPI using a generic return parameter 9 25
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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