Solved

How to use GUIDs in C# code

Posted on 2013-01-18
6
347 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connecting database in Xamarin? 6 54
Syntax error 9 45
Exit the loop 4 40
How can my static class become undefined?? 8 24
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now