Click here to monitor SSC

Andras

Software Architect - Red Gate Software

SQL Server 2008: new data types and .Net 2 with and without SP1

Published Friday, June 20, 2008 1:05 PM

SQL Server 2008 has introduced a few new data types, among others the new date types, like date, time, datatime2 and datetimespan. Because .Net 2 was released before SQL Server 2008 has introduced these data types, there are no classes that map to these new types in .Net 2.

But this has changed with .Net 2 SP1, which introduces the DateTimeOffset structure.

So, what should we expect when we run an application without SP1?
When we connect to SQL Server 2008 using .Net 2, the version of the runtime of the connecting application will determine the .Net data type that is returned to the user. For example, for a SQL datetimespan column without  .Net SP1 we get back a string, with SP1 we get a DateTimeOffset. Note that this happens even if you compile your application on a machine with SP1. So even though you have compiled and tested your application on .Net 2 SP1, a customer who runs the client without SP1 may experience a different behaviour. So what are these differences:

We will use a simple table that contains the new date data types:

CREATE TABLE someProblematicTypes
    (
      c_date date
    , c_time time
    , c_datatime2 datetime2
    , c_datetimeoffset datetimeoffset
    )

Insert a single row:

INSERT  INTO someProblematicTypes
VALUES  (
          GETDATE()
        , GETDATE()
        , GETDATE()
        , GETDATE() )

Then we create a C# application that uses an SqlDataReader  to retrieve the information about the data stored in this table. We compile this on a machine with .Net2 SP1, and run it on two machines, one with .Net 2 SP1 and one without. The instance of the SqlDataReader returns an object for the various columns, and the values and their types for the above table are:


Without SP1:

Column c_date
   Value:2008-06-20
   Type :System.String
Column c_time
   Value:2008-06-20
   Type :System.String
Column c_datatime2
   Value:2008-06-20 12:11:28.6000000
   Type :System.String
Column c_datetimeoffset
   Value:2008-06-20 12:11:28.6000000 +00:00
   Type :System.String


With SP1:

Column c_date
   Value:20/06/2008 00:00:00
   Type :System.DateTime
Column c_time
   Value:20/06/2008 00:00:00
   Type :System.DateTime
Column c_datatime2
   Value:20/06/2008 12:11:28
   Type :System.DateTime
Column c_datetimeoffset
   Value:20/06/2008 12:11:28 +00:00
   Type :System.DateTimeOffset


That the results are different, and let's not forget that it is the same executable that is being run. Without .Net2 SP1 we are getting strings instead of DateTime and the new DateTimeOffset. This could upset the application, and if our application was expecting a DateTime type, and got a string, we could easily get an invalid cast exception.

Also, we must be careful when we use the new DateTimeOffset type in our code. The application that is compiled and tested on a machine with SP1 will by default start on a client machine that does not have .Net 2 SP1. However, when it gets to the new data type, it will throw an unknown type exception. Unfortunately, since .Net 2 SP1 is relatively new (released November 2007), many have not upgraded to it. There are two general solutions to this. Either force the your customers to upgrade to .Net 2 SP1, or your application must be prepared for the above type differences and must be tested both with and without .Net 2 SP1.
 

by András

Comments

 

Jason Haley said:

June 20, 2008 9:14 AM
 

.NET Developer Notes said:

Link: SQL Server 2008: new data types and .Net 2 with and without SP1
June 25, 2008 3:20 AM
 

RobKraft said:

Wow.  Thanks for the info András!  I suspect this is going to cause some developers several days of research to determine why their app behaves differently on different computers.  Hopefully most deployments can require .Net 20 SP1 to get consistent behavior.
June 25, 2008 7:41 AM
 

Recent URLs tagged Datatypes - Urlrecorder said:

August 31, 2008 1:45 AM
You need to sign in to comment on this blog

About András

András Belokosztolszki is a software architect at Red Gate Software Ltd. He is a frequent speaker at many UK user groups and events (VBUG, NxtGen, Developer’s Group, SQLBits). He is primarily interested in database internals and database change management. At Red Gate he has designed and led the development of many database tools that compare database schemata and enable source control for databases (SQL Compare versions 4 to 7), refactor databases (SQL Refactor) and show the history of databases by analyzing the transaction log (SQL Log Rescue). András has a PhD from Cambridge and an MSc and BSc from ELTE, Hungary. He is also a MCSD and MCPD Enterprise. See his articles on simple-talk.
<June 2008>
SuMoTuWeThFrSa
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across, and start getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...

Converting String Data to XML and XML to String Data
 We all appreciate that, in general, XML documents or fragments are held in strings as text markup. In... Read more...