Generate GUID in c #. NET

Source: Internet
Author: User

GUID (globally unified identifier) refers to the number generated on a machine, which ensures that all machines in the same time and space are unique. Generally, the platform provides APIs for generating guids. The generation algorithm is very interesting. It uses Ethernet Card addresses, nanoseconds, chip ID codes, and many possible numbers. The only drawback of GUID is that the generated result string is large ."

1. a guid is a 128-bit integer (16 bytes). When using a unique identifier, you can use this integer between all computers and networks.

2. The GUID format is "xxxxxxxx-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 duplicate GUID value is generated on any two computers in the world (Koffer Note: it should be on the Earth. GUID is used to assign a unique identifier to a network or system with multiple nodes and computers.

4. On Windows, GUID is widely used: Registry, class and interface identification, database, or even automatically generated machine name and directory name.
 

Use GUID in. NET

When Windows developers need a Unique value, they usually 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). When using a unique identifier, you can use this integer between all computers and networks.

This article explains how the. NET Framework creates its own GUID for you as much as possible.

What you see

GUIDs is used in the entire Windows environment. When you carefully read the Registry in a Windows system, you can see that GUIDs is widely used for unique identification programs. Specifically, they are used as Ids of programs in the HKEY_CLASSES_ROOT section (AppID key ).

This is a typical GUID format:

936DA01F-9ABD-4d9d-80C7-02AF85C822A8

Generate a GUID in. NET.

Processing a unique identifier makes it easier to store and obtain information. This function is particularly useful when processing a database because a GUID can operate on a primary key.

Similarly, SQL Server well integrates the usage of GUID. The SQL Server Data Type uniqueidentifier can store a GUID value. You can use the NEWID () function to generate this value in SQL Server, or you can generate a GUID outside SQL Server, and then manually insert this value.

In. NET, the latter method is more direct .. The basic System class in the. NET Framework includes the GUID value type. In addition, this value type contains the method for processing the GUID value. Specifically, the NewGUID method allows you to easily generate a new GUID.

The following C # command line program describes the use process:
Using System;
Namespace DisplayGUID {
Class GuidExample {
Static void Main (string [] args ){
GenerateGUID ();
}
Static void GenerateGUID (){
Console. WriteLine ("GUID:" + System. Guid. NewGuid (). ToString ());
}}}

The following is the output of this program: (although the GUID between different systems is changed .)

GUID: 9245fe4a-d402-451c-b9ed-9c1a03167482

Here is the same code when VB. NET is used:

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 J # is used:

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 preceding example uses the NewGuid function of the System. Guid space name to return a value. (If you use this code in Visual Basic, you should thank this method for its simplicity .)

At this point, you can see that GUID is a good function, but where to use them in the program, and how to use them?

Use a GUID in the program

A guid can be used to operate a primary key in the background database. The following code uses a GUID to store information in a background database, which contains the following columns:

Pk_guid-uniqueidentifier Data Type
Name-nvarchar Data Type
In this case, a simple Windows form containing a text box appears. When the button is selected, the data in the text box is inserted into the database. You can use the program code to generate a GUID and store it in other columns:

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. That is, the GUID is assigned to a class or interface as an attribute. The standard attribute syntax can be used to implement this process: This article is published at www.bianceng.cn.

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-9c1a04107482")]
Class GuidExample {
Static void Main (string [] args ){
GenerateGUID ();
}
Static void GenerateGUID (){
Console. WriteLine ("GUID:" + System. Guid. NewGuid (). ToString ());
}}}
 

GUID is always convenient
For various aspects of program development,. NET Framework simplifies the process of establishing and processing the GUID value. Where the. NET program needs it, this function can easily generate a unique value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.