Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uniqueidentifier in SQL becomes lower case in c#

I have column(uniqueidentifier) in SQL which stored guid. I see that its in upper case. But when the data is returned through SP to C# code it becomes lower case

I am using entity framework in data access. What could be the reason and can i avoid this conversion?

like image 460
ravi shanker Avatar asked Jun 05 '13 10:06

ravi shanker


People also ask

What is the Uniqueidentifier datatype in SQL?

The globally unique identifier (GUID) data type in SQL Server is represented by the uniqueidentifier data type, which stores a 16-byte binary value. A GUID is a binary number, and its main use is as an identifier that must be unique in a network that has many computers at many sites.

How do you convert Uniqueidentifier?

The following example converts a uniqueidentifier value to a char data type. DECLARE @myid uniqueidentifier = NEWID(); SELECT CONVERT(CHAR(255), @myid) AS 'char'; The following example demonstrates the truncation of data when the value is too long for the data type being converted to.

Can SQL be written in lowercase?

All Caps SQL Commands For readability, all SQL commands should be written in uppercase letters. This allows the reader to identify the keywords in the SQL statement and easily determine what the query is executing.

Is the Uniqueidentifier the primary key?

A table with a UNIQUEIDENTIFIER column. The primary key in this table is of type UNIQUEIDENTIFIER . The values are generated by the NEWID function. With this setup, the primary key values are autogenerated, just like IDENTITY.


2 Answers

If you using Entity Framework, uniqueidentifier data will convert to Guid.

The value of this Guid, represented as a series of lowercase hexadecimal digits in the specified format.

If you need consistency in your application you can use one format when you get string out of Guid.

Check Guid.ToString Method (String) for available formats

There can be a place where you get guid as string from database so that will be infinity upper case. ( check stored procedures Views etc..)

To avoid this issue you have to make sure that return uniqueidentifier as it is, don't convert to varchar and also follow one stranded format when converting to string.

For other operations like comparing etc..You can use Guid operators..

like image 103
Damith Avatar answered Sep 28 '22 04:09

Damith


I suspect that SQL Server stores a uniqueidentifier as a 128-bit integer and then converts it to hexadecimal for display. As Alexander says, it doesn't matter whether it displays this hexadecimal value in upper-case or lower-case as they represent the same thing.

If it matters to your application, then you can convert it to upper-case in C# without a problem.

like image 25
Paul Spangle Avatar answered Sep 28 '22 05:09

Paul Spangle