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
When Windows developers need a unique value, they typically use a globally unique identifier (GUID, globally unique Identifier). Microsoft uses the GUID term to represent this unique value, which can identify an entity, such as a Word document.
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.
This article will explain. NET Framework does its best to create your own GUID for you.
What you see.
GUIDs is used throughout the Windows environment. When you read the registry carefully in a Windows system, you can see that GUIDs is widely used in the unique identification program. Specifically, they are concentrated in the HKEY_CLASSES_ROOT section (AppID key) as the IDs of the programs.
This is the format of a typical GUID:
936da01f-9abd-4d9d-80c7-02af85c822a8
Generate a GUID in. net
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 following C # command-line program illustrates the use of this procedure:
Using System;
Namespace Displayguid {
Class Guidexample {
static void Main (string[] args) {
Generateguid ();
}
static void Generateguid () {
Console.WriteLine ("GUID:" + System.Guid.NewGuid (). ToString ());
} } }
The output of this program is as follows: (although the GUID varies between systems.) )
guid:9245fe4a-d402-451c-b9ed-9c1a04247482
Here's the same code when using vb.net:
Module Builderexamples
Sub Main ()
Generateguid ()
End Sub
Public Sub Generateguid ()
Console.WriteLine ("GUID:" + System.Guid.NewGuid (). ToString ())
End Sub
End Module
Here is the same code when using J #:
Package builderexamples;
Import System.Console;
public class Guidexample {
Public guidexample () {}
public static void Main (string[] args) {
Generateguid ();
}
static void Generateguid () {
Console.WriteLine ("GUID:" + System.Guid.NewGuid (). ToString ());
} }
The example above uses the NewGuid function to system.guid the space name to return a numeric value. (If you use this code in Visual Basic, you should be thankful for the simplicity of this approach.) )
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. A GUID can be generated by program code and stored in another column:
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using Microsoft.ApplicationBlocks.Data;
Namespace Guiddbexamplecsharp {
public class Frmbuildertest:form {
Private Label Lblname;
Private TextBox txtname;
Private Button Btninsert;
Private Container components = null;
Public Frmbuildertest () {
InitializeComponent ();
}
static void Main () {
Application.Run (New Frmbuildertest ());
}
private String Generateguid () {
Return System.Guid.NewGuid (). ToString ();
}
private void Btninsert_click (object sender, System.EventArgs e) {
String cs = "server= (local); Initial catalog=northwind;integrated
Security=sspi ";
using (SqlConnection conn = new SqlConnection (CS)) {
try {
String Sqlinsert = "INSERT into Dbo.tblbuildertest (Pk_guid, [name]) VALUES ('"
+ System.Guid.NewGuid (). ToString () + "', '" + txtName.Text + "')";
Conn. Open ();
Sqlhelper.executenonquery (conn, CommandType.Text, Sqlinsert);
catch (Exception ex) {
System.Console.Write ("Error:" + ex.) message);
} } } } }
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 procedure:
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:
Using System;
Using System.Runtime.InteropServices;
Namespace Displayguid {
[Guid ("9245fe4a-d402-451c-b9ed-9c1a04247482")]
Class Guidexample {
static void Main (string[] args) {
Generateguid ();
}
static void Generateguid () {
Console.WriteLine ("GUID:" + System.Guid.NewGuid (). ToString ());
} } }
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.