1
我有以下方法,此刻它返回整個sql字符串。我將如何執行以下操作。如何從nhibernate調用sql字符串
using (ITransaction transaction = session.BeginTransaction())
{
string sql =
string.Format(
@"DECLARE @Cost money
SET @Cost = -1
select @Cost = MAX(Cost) from item_costings
where Item_ID = {0}
and {1} >= Qty1 and {1} <= Qty2
RETURN (@Cost)",
itemId, quantity);
string mystring = session
.CreateSQLQuery(sql)
.ToString();
transaction.Commit();
return mystring;
}
//編輯
這裏是一個使用標準
using (ISession session = NHibernateHelper.OpenSession())
{
decimal cost = session
.CreateCriteria(typeof (ItemCosting))
.SetProjection(Projections.Max("Cost"))
.Add(Restrictions.Eq("ItemId", itemId))
.Add(Restrictions.Le("Qty1", quantity))
.Add(Restrictions.Ge("Qty2", quantity))
.UniqueResult<decimal>();
return cost;
}
謝謝迪戈,結束了你的建議,並做好了。 – frosty 2010-05-15 12:21:20