A GUID (global uniform identifier) is a number generated on a machine that guarantees that all machines in the same time and space are unique. Typically, the platform provides the API to generate the GUID. The generation algorithm is interesting, using the Ethernet card address, nanosecond time, chip ID code and many possible numbers. The only drawback to the GUID is that the resulting string will be larger. ”
1. A GUID is a 128-bit integer (16 bytes), and in the case of a unique identifier, you can use this integer between all computers and networks.
2. The format of the GUID is "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where each x is a hexadecimal number in the range of 0-9 or a-f. For example, 337C7F2B-7A34-4F50-9141-BAB9E6478CC8 is a valid GUID value.
3. No two computers in the world (Koffer Note: should be on earth) will generate duplicate GUID values. GUIDs are primarily used to assign identifiers that must be unique in a network or system that has multiple nodes, multiple computers, and more than one computer.
4. On the Windows platform, GUIDs are widely used: Registry, class and interface identifiers, databases, even automatically generated machine names, directory names, and so on.
. NET using GUIDs
GUIDs are widely used in. NET, and the. NET Framework provides a dedicated GUID infrastructure.
Common methods for GUID structure include:
1) Guid.NewGuid ()
To generate a new GUID unique value
2) guid.tostring ()
Converts a GUID value into a string for easy handling
3) constructor Guid (string)
The GUID structure is generated by string, where string can be uppercase or lowercase, can contain both ends of the delimiter "{}" or "()", or even omit the middle "-", the GUID structure has a lot of constructors, other construction usages are not commonly used.
Class Guidconverter can be used in the. NET Framework to provide a type converter that converts a GUID structure to a variety of other representations.
Generate a GUID in C #
Processing a unique identifier makes it easier to store and obtain information. This feature is especially useful in working with a database because a GUID can manipulate a primary key.
Similarly, SQL Server is well integrated with the purpose of GUIDs. SQL Server data type uniqueidentifier can store a GUID value. You can generate this value in SQL Server by using the NEWID () function, or you can generate a GUID outside of SQL Server and then manually insert the value.
In. NET, the latter approach appears more straightforward. The base system class in the. NET Framework includes GUID numeric types. In addition, this numeric type contains methods for handling GUID values. In particular, the NewGuid method allows you to easily generate a new GUID.
Usingnamespace displayguid{ class program { staticvoid Main (string[] args) { generateguid (); } Static void Generateguid () { Console.WriteLine ("GUID:" + System.Guid.NewGuid (). ToString ()); }}}
View Code
The following is the output of this program: (Although the GUID between the different systems is changed.) )
guid:9245fe4a-d402-451c-b9ed-9c1a04247482
The example above uses the NewGuid function of the System.Guid space name to return a numeric value. At this point, you can see that the GUIDs are a good feature, but where do you use them in the program, and how do you use them?
Using a GUID in your program
A GUID can manipulate a primary key in the background database. The following code uses a GUID to store information in a background database that contains the following columns:
Pk_guid-uniqueidentifier Data types
Name-nvarchar Data types
A simple Windows Form that contains a text box appears. When the button is selected, the data in the text box is inserted into the database in the Name column. Program code allows you to generate a GUID and store it in the Pk_guid column:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;namespaceguidsqldbexample{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidBtninsert_click (Objectsender, EventArgs e) { string_str ="server= (local); Initial catalog=testguid;integrated Security=sspi"; using(SqlConnection conn =NewSqlConnection (_STR)) { Try { string_sqlinsert ="INSERT into dbo. Guid (pk_guid, name) VALUES ('"+ System.Guid.NewGuid (). ToString () +"', '"+ txtName.Text +"')"; Conn. Open (); SqlCommand _cmd=NewSqlCommand (_sqlinsert, conn); _cmd. ExecuteNonQuery (); } Catch(Exception ex) {System.Console.Write ("Error:"+Ex. Message); } } } }}
View Code
Another GUID program assigns a unique identifier to a. NET class or interface, that is, the GUID is assigned to a class or interface as a property. You can use the standard attribute syntax to implement this process:
We can extend the first example to assign a GUID. The System.Runtime.InteropServices space name must be referenced to use the GUID property. The following C # code implements this process:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Runtime.InteropServices; namespaceguidtest {[Guid ("9245fe4a-d402-451c-b9ed-9c1a04247482")] classProgram {Static voidMain (string[] args) {Generateguid (); } Static voidGenerateguid () {Console.WriteLine ("GUID:"+System.Guid.NewGuid (). ToString ()); } }}
View Code
GUIDs are always handy.
For all aspects of program development, the. NET framework simplifies the process of establishing and handling GUID values. This feature makes it easy to generate unique values where a. NET program is needed.
Example:
Static voidMain (string[] args) { //use NewGuid static method to get a new instance of a GUIDSystem.Guid Guid =System.Guid.NewGuid (); System.Console.WriteLine (GUID. ToString ()); // //The equality and inequality operators are the GUID structure of their own operators, do not mix with the operator "= =", "! = "//System.Guid Guid2=GUID; if(GUID = = Guid2)//Use the equality operator to determine whether two GUIDs are equal{System.Console.WriteLine (GUID). ToString ()+"equals"+Guid2. ToString ()); } Guid2=System.Guid.NewGuid (); if(GUID! = Guid2)//Use the equality operator to determine whether two GUIDs are unequal{System.Console.WriteLine (GUID). ToString ()+"Not equal to"+Guid2. ToString ()); } System.Console.ReadLine (); }
View Code
Use of GUIDs in. Net