Solved

How to use GUIDs in C# code

Posted on 2013-01-18
6
354 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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