?
Solved

How to use GUIDs in C# code

Posted on 2013-01-18
6
Medium Priority
?
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 20

Assisted Solution

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

Assisted Solution

by:Dale Burrell
Dale Burrell earned 336 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 332 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Assisted Solution

by:Russ Suter
Russ Suter earned 332 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 332 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 332 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

649 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