NHibernate DateTime for query, Overflow exception caused by string format

半城伤御伤魂 提交于 2020-01-16 08:50:11

问题


NHibernate is generating the following SQL which is not supported by Firebird;

where  (struct_cas0_.DELETED IS NULL)
       and struct_cas0_.ACCOUNT_ID = 372 /* @p0 */
       and struct_cas0_.DATE_RECORD <= '2005-01-01T00:00:00.00' /* @p1 */
       and struct_cas0_.DATE_RECORD >= '2006-12-31T00:00:00.00' /* @p2 */

The above SQL fails in firebird with an error "Overflow occurred during data type conversion. Conversion error from string '2005-01-01T00:00:00.00'"

If we remove the 'T' from the query, Firebird executes the query without problem;

where  (struct_cas0_.DELETED IS NULL)
       and struct_cas0_.ACCOUNT_ID = 372 /* @p0 */
       and struct_cas0_.DATE_RECORD <= '2005-01-01 00:00:00.00' /* @p1 */
       and struct_cas0_.DATE_RECORD >= '2006-12-31 00:00:00.00' /* @p2 */

Is there a way we can have NHibernate remove the 'T' when converting DateTime to a queryable string?

An additonal question is now raised after some reasearch. It appears that FireBird does not support the combined date and fime format DateTime format (ISO8601) http://en.wikipedia.org/wiki/ISO_8601 with a time discrimiator character ("T"). Can this be confirmed as I don't understand why a database would fail to support such a standard (sortable) date format?

If the "T" is dropped, the date is supported.


回答1:


Are you sure you have the correct driver, etc.. as per this question How do I setup NHibernate with Visual Studio and Firebird? ?

Also, make sure what datatype you have in the DB and in the mapping. Are they exactly the same? (for example, are both non-nullable) ?

Otherwise, it may be a bug in NHibernate itself. Best solution will be upgrading to NHibernate 3.0 (or better offf latest trunk version, don't worry, those are usually stable). If it still a bug, then make a JIRA issue in there and it may get fixed quick.




回答2:


Format DateTime yourself to String. http://msdn.microsoft.com/en-us/library/az4se3k1(v=VS.100).aspx

Also I think the DateTime Format produced by Nhibernate may depend on regional settings. Try changing Thread culture.

Try to execute pure ADO.NET command from NHConnection with DateTime parameter, to check if it is FireBird .ADO driver messing DateTime format with culture. ISession.Connection.CreateCommand()

NHibernate should use parameterized Ado.net IDbCommand, so it's hardly that this is Nhibernate problem. I don't think Nhibernate format your datetime to string anywhere in it's code, at least it should not.




回答3:


NHibernate does not convert parameters to strings; that's the ADO.NET provider's responsibility.

Try the following:

var connection = new FbConnection(theConnectionString);
var command = connection.CreateCommand();
var parameter = command.CreateParameter();
command.CommandText = "select something from that_table where date_record = @p";
parameter.Name = "p";
parameter.Value = DateTime.Today;
command.Parameters.Add(parameter)
connection.Open();
command.ExecuteReader();

If it fails, there's your bug.



来源:https://stackoverflow.com/questions/5037711/nhibernate-datetime-for-query-overflow-exception-caused-by-string-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!