GUIDs in C# / SQL

GUID pronounced as goo’id - Globally unique identifier is a 128-bit integer that can be used to uniquely identify something. If you want to identify the large collection of data GUID is the best approach in my view. Also you can use the autoincremental integer to identify records, this is the basic approach programmer’s use.

GUID method can be found under the System namespace and it also provides some overloads. Here is how we can use the GUID overloads:

System.Guid.NewGuid().ToString();    //c93ded50-7cce-4f3a-b8aa-5a1a34d80b14
System.Guid.NewGuid().ToString("N"); //636bdfd2bbc541229fbc6b869995ce6f
System.Guid.NewGuid().ToString("D"); //45e602b6-0904-47ac-a764-a5522f56f40f
System.Guid.NewGuid().ToString("B"); //{68f55508-f72c-4a38-b96b-f493c3c5adb9}
System.Guid.NewGuid().ToString("P"); //(1c2e52a7-e384-445e-b80a-e8704c6ecbd6)

To create GUID in SQL Server use the simple select statement:

SELECT NEWID()
comments powered by Disqus