Project problem summary 2: Is GUID case sensitive ?, Guid case
Problem description:
Recently, when I was working on a project, I encountered a problem. I used the course ID queried from the basic system as a parameter to query the test information in the evaluation system, but I couldn't find anything, after debugging for half a day, I don't know why.
Problem Analysis:Calm down and think about it. It is certain that the logic and implementation of the program code are no problem, so it is a data problem. In this case, a data problem should be caused by a database problem. During the single-step debugging, we checked the values of various variables and parameters and finally found out the problem, let's take a look at the data type of CourseId stored in the databases of the two systems, for example:
The left side is the foundation, and the right side is the examination. Let's take a look at how my code implements the process described in the Problem description:
When thinking about it, I realized a problem. What is GUID ?? So Baidu: The Globally Unique Identifier (GUID, Globally Unique IDentifier) is also called UUID (Universally Unique Identifier ). GUID is a 128-bit numeric identifier generated by an algorithm. GUID is mainly used in networks or systems with multiple nodes and multiple computers. Ideally, no computer or computer cluster generates two identical guids. The total number of guids reaches 2 ^ 128 (3.4 × 10 ^ 38), so the probability of randomly generating two identical guids is very small, but not 0. The term GUID sometimes refers to Microsoft's UUID standard implementation. What is the case sensitivity of digital identifiers! Alas, reading is rare! Believe it or not, we can use the code for verification:
// Verify that the GUID is case sensitive. Guid g1 = new Guid ("6d025d37-a957-44b1-9e25-50d6bfd57a39"); Guid g2 = new Guid ("Courier"); Console. writeLine (g1.Equals (g2); // return true Console. writeLine (Object. referenceEquals (g1, g2); // return false // use the tostring function to convert the GUID to the string type. Observe that their values are string strg1 = g1.ToString (); string strg2 = g2.ToString (); Console. writeLine (strg1.Equals (strg2); // return true Console. writeLine (strg1); Console. writeLine (strg2); // However, if your database storage type is varchar, it is different from string str1 = "canonical"; string str2 = "6d025d37-a957-44b1-9e25-50d6bfd57a39"; Console. writeLine (str1.Equals (str2); // return false Console. read ();The uniqueidentifier mentioned above is the storage type of GUID in the database. They are actually a thing. As a result, it becomes much clearer.
Solution:There are two solutions: one is to make the data types of the two databases consistent, which is a fundamental solution; if you do not want to modify the database, you can use the data type of varchar to store the GUID string, but make sure that it must be in lowercase letters. Otherwise, when querying based on its field value, the string cannot match the string produced by the ToString () method of the guid. Therefore, the query result is definitely incorrect. We recommend that you unify the data type.
Summary:In fact, if you think about it carefully, it is still basic things that are not solid. I don't know the GUID concept at all, so there will be questions about the title. If I know clearly that GUID is a digital identifier generated by an algorithm with a binary length of 128 bits, I may quickly find out where the problem is.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.