1

I'm trying to code an object based on an SQL table, and up until this point, I've only dealt with strings/dates/int/long and so on, things that are relatively easily translatable to C#. I went to start coding my fields in c# for an asp page, and I noticed there is a unique identifier data type, and also an xml datatype. I am unsure as to how to proceed coding these datatypes into my fields, and how how to data will move inside of my project. Can anyone help shed some light on this, and possible get me started, at least with coding my object?

Kyle Erickson
  • 1,558
  • 1
  • 11
  • 14
  • It sounds like you're talking about the SQL [`uniqueidentifier`](http://technet.microsoft.com/en-us/library/ms187942.aspx) type. This corresponds conceptually to a `System.Guid` in .NET. I'm not sure the best way to convert between them. See also http://stackoverflow.com/questions/1435908/c-sharp-guid-and-sql-uniqueidentifier – harpo Apr 05 '14 at 16:21

1 Answers1

0

As @harpo said, a UNIQUEIDENTIFIER type in SQL is translated to a System.Guid in .NET. If you are using ado.net and a dataReader you can convert the column like so:

    Guid myUniqueIdentifier = reader["MyUniqueIdentifierColumn"] != DBNull.Value && reader["MyUniqueIdentifierColumn"] != null ? Guid.Parse(reader["MyUniqueIdentifierColumn"].ToString()) : Guid.Empty;

for an XML column you can just use a string dataType in c#. If you want to be able to get values from the XML then you need to create an object that mirrors the structure of the XML and deserialize the object using XmlSerializer or DataContractSerializer. For more information on that see this SO thread: https://stackoverflow.com/a/14723129/2488939

Community
  • 1
  • 1
The Mahahaj
  • 696
  • 6
  • 8