Note using 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. 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
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}
The following is the output of this program: (although the GUID between different systems is changed .)
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. 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 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-9c1a04107482")]
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.
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.