sql-servernhibernate

Using SQL Server 2008 Geography types with nHibernate's CreateSQLQuery


I am trying to issue a SQL update statement with nHibernate (2.0.1GA) like this:

sqlstring = string.Format("set nocount on;update myusers set geo=geography::Point({0}, {1}, 4326) where userid={2};", mlat, mlong, userid);
_session.CreateSQLQuery(sqlstring).ExecuteUpdate();

However I receive the following error: 'geography@p0' is not a recognized built-in function name.

I thought CreateSQLQuery would just pass the SQL I gave it and execute it...guess not. Any ideas on how I can do that within the context of nHibernate?


Solution

  • I'm pretty sure I can tell you what is happening, but I don't know if there is a fix for it.

    I think the problem is that the ':' character is used by NHibernate to create a named parameter. Your expression is getting changed to:

    set nocount on;update myusers set geo=geography@p0({0}, {1}, 4326) where userid={2};
    

    And @p0 is going to be a SQL variable. Unfortunately I can't find any documentation for escaping colons so they are not treated as a named parameter.

    If an escape character exists (my quick skim of the NHibernate source didn't find one; Named parameters are handled in NHibernate.Engine.Query.ParameterParser if you want to spend a little more time searching), then you could use that.

    Other solutions: