andreazevedo wrote:
Ok...
So how can i execute a SELECT and return to me all dates in a table grouped by dates...
The T-SQL query would be like that:
SELECT date FROM table GROUP BY date
How can i do that with NHibernate?
Code:
string sql = "SELECT date FROM table GROUP BY date";
ISession session = mSessionProvider.GetSession();
IQuery sqlQuery = session.CreateSQLQuery(sql, "vw", pType);
IList result = sqlQuery.List();
But pay attention with the date format. With the Sql-Server 2000 / 2005 you may run in problems if the Windows client and the SqlServer use different languages. In our environment we have an english Sql-Server and a German Windows. We solve this problem by using the CONVERT() function and translating the date value to a string with american date format. This works with any language installation of the Sql-Server.
Code:
private string GetSqlDate(DateTime pDate)
{
// translate date to US-format
return "CONVERT(datetime, '" + pDate.ToString("MM/dd/yyyy") + "', 101)";
}
Quote:
And can I execute procedures with NHibernate?
In principle it should work with an SQLQuery. The problem is, that you don't have explicit support for Stored Procedures in NHibernate. If you want to pass parameters you have to concat them in the SQL. We have done some tests but came to the decision to call stored procedures directly via ADO.NET. You can use the same ConnectString as NHibernate:
Code:
protected string GetHibernateConnectString()
{
// Use the same DB connectstring as NHibernate (read from App.config)
NameValueCollection dbConfig = (NameValueCollection)ConfigurationSettings.GetConfig("nhibernate");
return dbConfig["hibernate.connection.connection_string"].ToString();
}
Regards
Klaus