Solved

How to use GUIDs in C# code

Posted on 2013-01-18
6
346 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 74

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

23 Experts available now in Live!

Get 1:1 Help Now