Use guid in C #

Source: Internet
Author: User

Use guid in C #

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. GenerateAlgorithmIt is interesting to use 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

GUID is widely used in. net, and. NET Framework provides a dedicated guid infrastructure.
Common guid structure methods include:
1) guid. newguid ()
Generate a new guid unique value
2) guid. tostring ()
Converts a guid value to a string for processing.
3) constructor GUID (string)
The GUID structure is generated by the string, where the string can be in upper or lower case. It can contain the delimiters "{}" or "()" at both ends. you can even omit the "-" in the middle. There are many constructors in the guid structure, and other constructors are not commonly used.

In. NET Framework, you can use the guidconverter class to provide a type converter that converts the guid structure with various other representations.

 

Generate a guid in C #.

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.

 

 

1 using system;
2 namespace displayguid
3 {
4 class Program
5 {
6 static void main (string [] ARGs)
7 {
8 generateguid ();
9}
10 static void generateguid ()
11 {
12 console. writeline ("guid:" + system. guid. newguid (). tostring ());
13}
14}
15}

 

Below isProgram(Although guids vary between different systems .)

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

The preceding example uses the newguid function of the system. guid space name to return a value. 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. BelowCodeA guid is used 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 name column of the database. You can use the program code to generate a guid and store it in the pk_guid column:

 

1 using system;
2 using system. Collections. Generic;
3 using system. componentmodel;
4 using system. Data;
5 using system. drawing;
6 using system. LINQ;
7 using system. text;
8 using system. Windows. forms;
9 using system. Data. sqlclient;
10
11 namespace guidsqldbexample
12 {
13 public partial class form1: Form
14 {
15 public form1 ()
16 {
17 initializecomponent ();
18}
19
20 private void btninsert_click (Object sender, eventargs E)
21 {
22 string _ STR = "Server = (local); initial catalog = testguid; Integrated Security = sspi ";
23 using (sqlconnection conn = new sqlconnection (_ Str ))
24 {
25 try
26 {
27 string _ sqlinsert = "insert into DBO. GUID (pk_guid, name) values ('"+ system. guid. newguid (). tostring () + "','" + txtname. text + "')";
28 conn. open ();
29 sqlcommand _ cmd = new sqlcommand (_ sqlinsert, Conn );
30_cmd. executenonquery ();
31}
32 catch (exception ex)
33 {
34 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. 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:

 

 

1 using system;
2 using system. collections. generic;
3 using system. LINQ;
4 using system. text;
5 using system. runtime. interopservices;
6
7 namespace guidtest
8 {
9 [GUID ("9245fe4a-d402-451c-b9ed-9c1a03167482")]
10 class Program
11 {
12 static void main (string [] ARGs)
13 {
14 generateguid ();
15}
16 static void generateguid ()
17 {
18 console. writeline ("guid:" + system. guid. newguid (). tostring ();
19}
20}
21}

 

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.

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.