Method for generating guid in C #

Source: Internet
Author: User

How do I generate a guid in C? We hope you can answer the following questions.

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. 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.

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

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. 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 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 ());

}}}

Format description

 

N 32-bit:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

For example, e92b8e30a6e541f6a6b9188230a23dd2

D 32-digit numbers separated by a hyphen:

XXXXXXXX-XXXX-xxxxxxxxxxxx

Such as: e92b8e30-a6e5-41f6-a6b9-188230a23dd2

B is a 32-bit number separated by a hyphen in braces:

{XXXXXXXX-XXXX-xxxxxxxxxxxx}

Example: {e92b8e30-a6e5-41f6-a6b9-188230a23dd2}

P contains a 32-bit number separated by a hyphen in parentheses:

(XXXXXXXX-XXXX-xxxxxxxxxxxx)

(E92b8e30-a6e5-41f6-a6b9-188230a23dd2)

Do you understand?

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.