Search   

Tuesday, January 06, 2009

ExtendingNDOProviders

Register  Login

 Menu  
 
   Print   

 Extending the NDO provider for Sql Server to support nvarchar(max)  
 


In Sql Server 2005 you can specify the expression 'max' as the length of a NVarChar column like that:

...
[FirstName] NVarChar(max) NULL
...

You can use that column type in NDO, if you alter the ColumnType property in the mapping file for a certain field to 'nvarchar(max)'. Note, that the value 'max' cannot be entered as ColumnLength property, since NDO expects an int value there.

The problem: The NDO provider for Sql Server shipped with pre SP 1 versions of NDO 1.2 can't recognize the type 'nvarchar(max)', so it throws an exception.

The solution: Writing a new NDO provider for Sql Server, which derives from the existing NDO provider. The new provider overrides the method "GetDbType(string)", which throws the exception. The new method checks in a case insensitive way, if the string parameter starts with "nvarchar". If this is the case, it returns SqlDbType.NVarChar. Otherwise it delegates the call to the base class.

Note, that NDO supports nvarchar(max) column types since version 1.2 SP 2. But this is a very good sample of how easy it is to alter the behavior of the NDO providers.

Source code: The source contains the provider and a little test application. See the comments in the source code of the TestApp project to get the project running on your system.

 

   Print