A GUID (global uniform identifier) is a number that is generated on a single machine and is guaranteed to be unique to all machines in the same space-time. Typically, the platform provides an API to generate GUIDs. The generation algorithm is interesting, using the Ethernet card address, nanosecond time, chip ID code, and many possible numbers. The only drawback to GUIDs is that the resulting string is 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 the network.
2. The GUID is formatted as "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", where each x is a hexadecimal number in the range 0-9 or a-f. For example: 337C7F2B-7A34-4F50-9141-BAB9E6478CC8 is a valid GUID value.
3. Any two computers in the world (Koffer Note: should be on Earth) will not generate duplicate GUID values. GUIDs are primarily used to assign identifiers that must be unique in a network or system that has more than one node or multiple computers.
4. On Windows platforms, GUIDs are widely used: Registry, class and interface identities, databases, and even automatically generated machine names, directory names, and so on.
. Use GUID in net
GUIDs are widely used in. NET, and the. NET Framework provides a dedicated GUID infrastructure.
Common methods of GUID structure include:
1) Guid.NewGuid ()
Generate a new GUID unique value
2) guid.tostring ()
Converts a GUID value to a string for easy processing
3 constructor Guid (string)
The GUID structure is generated by string, where a string can be uppercase or lowercase, can contain both ends of the delimiter "{}" or "()", or even omit the middle "-", there are many constructors for the GUID structure, and other construction usages are not commonly used.
Class Guidconverter can be used in the. NET Framework to provide type converters that convert the GUID structure to a variety of other representations.
Generate a GUID in C #
Processing a unique identifier makes it easier to store and access information. This functionality becomes particularly useful in processing a database, because a GUID can manipulate a primary key.
Similarly, SQL Server integrates the use of GUIDs nicely. 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 this value.
In. NET, the latter approach appears more straightforward. The basic system class in the. NET Framework includes the GUID numeric type. In addition, this numeric type contains methods for handling GUID values. In particular, the NewGuid method allows you to easily generate a new GUID.
The code is as follows |
Copy Code |
1using System; 2namespace Displayguid 3{ 4 Class Program 5 { 6 static void Main (string[] args) 7 { 8 Generateguid (); 9} Ten static void Generateguid () 11 { Console.WriteLine ("GUID:" + System.Guid.NewGuid (). ToString ()); 13} 14} 15}
|
The output of this program is as follows: (although the GUID varies between systems.) )
guid:9245fe4a-d402-451c-b9ed-9c1a04247482
The example above uses the NewGuid function to system.guid the space name to return a numeric value. At this point, you can see that GUIDs are a good feature, but where do you use them in your 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 type
Name-nvarchar data type
This shows a simple Windows Form that contains a text box. When the button is selected, the data in the text box is inserted into the database name column. A GUID can be generated by program code and stored in the Pk_guid column:
The code is as follows |
Copy Code |
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Windows.Forms; 9using System.Data.SqlClient; 10 11namespace Guidsqldbexample 12{ Partial class Form1:form 14 { Public Form1 () 16 { InitializeComponent (); 18} 19 private void Btninsert_click (object sender, EventArgs e) 21 { String _str = "server= (local); Initial catalog=testguid;integrated Security=sspi"; By using (SqlConnection conn = new SqlConnection (_STR)) 24 { Try 26 { String _sqlinsert = "INSERT into dbo." Guid (pk_guid, name) VALUES (' + System.Guid.NewGuid (). ToString () + "', '" + txtName.Text + "')"; Conn. Open (); SqlCommand _cmd = new SqlCommand (_sqlinsert, conn); _cmd. ExecuteNonQuery (); 31} catch (Exception ex) 33 { System.Console.Write ("Error:" + ex.) message); 35} 36} 37} 38} 39 40} |
Another GUID program assigns a unique identifier to a. NET class or interface, which means that the GUID is assigned to a class or interface as a property. You can use standard property syntax to implement this process: the
We can extend the first example to assign a GUID. The System.Runtime.InteropServices space name must be referenced to use the GUID attribute. The following C # code implements this process:
The code is as follows |
Copy Code |
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Runtime.InteropServices; 6 7namespace Guidtest 8{ 9 [Guid ("9245fe4a-d402-451c-b9ed-9c1a04247482")] Class Program 11 { static void Main (string[] args) 13 { Generateguid (); 15} static void Generateguid () 17 { Console.WriteLine ("GUID:" + System.Guid.NewGuid (). ToString ()); 19} 20} 21} |
GUIDs are always convenient
for all aspects of program development, the. NET framework simplifies the process of establishing and processing GUID values. This feature can easily generate unique values where. NET programs are needed.