- public void ExecuteNonQuery(string sql)
- {
- ISession session = null;
- ITransaction transaction = null;
- try
- {
- session = SessionHelper.OpenSession();
- transaction = session.BeginTransaction();
- IDbCommand command = session.Connection.CreateCommand();
- transaction.Enlist(command);
- command.CommandText = sql;
- command.ExecuteNonQuery();
- transaction.Commit();
- }
- catch (Exception ex)
- {
- if (transaction != null)
- {
- transaction.Rollback();
- }
- throw ex;
- }
- finally
- {
- if (session != null)
- {
- session.Close();
- }
- }
- }
- public DataSet ExecuteDataset(string sql)
- {
- ISession session = null;
- DataSet ds = new DataSet();
- try
- {
- session = SessionHelper.OpenSession();
- IDbCommand command = session.Connection.CreateCommand();
- command.CommandText = sql;
- IDataReader reader = command.ExecuteReader();
- DataTable result = new DataTable();
- DataTable schemaTable = reader.GetSchemaTable();
- for (int i = 0; i < schemaTable.Rows.Count; i++)
- {
- result.Columns.Add(schemaTable.Rows[i][0].ToString());
- }
- while (reader.Read())
- {
- int fieldCount = reader.FieldCount;
- object[] values = new Object[fieldCount];
- for (int i = 0; i < fieldCount; i++)
- {
- values[i] = reader.GetValue(i);
- }
- result.Rows.Add(values);
- }
- ds.Tables.Add(result);
- }
- catch (Exception ex)
- {
- Debug.Assert(false);
- }
- finally
- {
- if (session != null)
- {
- session.Close();
- }
- }
- return ds;
- }