/// <summary> /// Get all records. /// </summary> /// <returns>Returns all records for the passed entity type.</returns> public ICollection <T> GetAll() { using (ISession session = NHibernateContext.GetCurrentSession()) { return(session.Query <T>().ToList()); } }
/// <summary> /// Get a record by ID. /// </summary> /// <param name="id">The ID.</param> /// <returns>The record which will be identified by the passed ID.</returns> public T Get(int id) { using (ISession session = NHibernateContext.GetCurrentSession()) { return(session.Get <T>(id)); } }
/// <summary> /// Find a category by name. /// </summary> /// <param name="name">The name.</param> /// <returns>Returns the corresponding category.</returns> internal Category FindByName(string name) { using (ISession session = NHibernateContext.GetCurrentSession()) { return(session .CreateCriteria <Category>() .Add(Restrictions.Eq("Name", name)) .UniqueResult <Category>()); } }
/// <summary> /// Find an account by name. /// </summary> /// <param name="name">The name.</param> /// <returns>Returns the corresponding account.</returns> internal ICollection <Account> FindByName(string name) { using (ISession session = NHibernateContext.GetCurrentSession()) { return(session .CreateCriteria <Account>() .Add(Restrictions.Eq("Name", name)) .List <Account>()); } }
/// <summary> /// Find an account by IBAN. /// </summary> /// <param name="iban">The International Bank Account Number.</param> /// <returns>Returns the corresponding account.</returns> public Account FindByIban(string iban) { using (ISession session = NHibernateContext.GetCurrentSession()) { return(session .CreateCriteria <Account>() .Add(Restrictions.Eq("IBAN", iban)) .UniqueResult <Account>()); } }
/// <summary> /// Update the passed record. /// </summary> /// <param name="record">The record.</param> public void Update(T record) { using (ISession session = NHibernateContext.GetCurrentSession()) { using (ITransaction transaction = session.BeginTransaction()) { session.Update(record); transaction.Commit(); } } }
/// <summary> /// Execute a SQL statement. /// </summary> /// <param name="sqlCommand">The SQL statement.</param> /// <param name="identity">The windows identity under which the statement should be running.</param> /// <returns>Returns a DataTable for the result.</returns> public DataTable ExecuteSql(string sqlCommand, WindowsIdentity identity = null) { var result = new DataTable(); try { var session = NHibernateContext.GetCurrentSession(); using (var transaction = session.BeginTransaction()) { var query = session.CreateSQLQuery(sqlCommand); var sqlResult = query.List(); foreach (var row in sqlResult) { if (row is object[] rowArray) { var i = 0; while (result.Columns.Count < rowArray.Length) { result.Columns.Add(new DataColumn()); i++; } foreach (var item in rowArray) { result.Rows.Add(item); } } else { if (result.Columns.Count == 0) { result.Columns.Add(new DataColumn()); } result.Rows.Add(row); } } // transaction.Commit(); } } finally { NHibernateContext.CloseSession(); } return(result); }