/// <summary> /// Start transaction /// </summary> /// <param name="isolationLevel">transaction level</param> /// <returns></returns> public virtual DbTransaction BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) { throwTransactionIsRunningException(); _tranactionStatus = TransactionStatus.Running; _dbConnection = NativeSql.GetConn(); _dbConnection.Open(); _dbTransaction = _dbConnection.BeginTransaction(isolationLevel); return(_dbTransaction); }
/// <summary> /// Betch insert,don't return id value /// </summary> /// <typeparam name="T">Entity</typeparam> /// <param name="isIdAutoGrow">Is id auto grow?</param> /// <param name="list">Entity list</param> public virtual void SaveOnly <T>(List <T> list, bool isIdAutoGrow = true, DbTransaction trans = null) where T : Entity { var sql = new StringBuilder(); var isNeedCommit = false; var isSupportBatch = ProviderManager.GetProvider(ProviderName).IsSupportBatch; if (isSupportBatch) { sql.Append(ProviderManager.GetProvider(ProviderName).BatchBegin); } else if (trans == null) { var conn = NativeSql.GetConn(); conn.Open(); trans = conn.BeginTransaction(); isNeedCommit = true; } foreach (var item in list) { item.Key = Key; var sqlT = item.GetSaveSql(isIdAutoGrow); if (isSupportBatch) { sql.Append(sqlT); } else { NativeSql.ExecuteNonQuery(trans, sqlT); } } if (isSupportBatch) { sql.Append(ProviderManager.GetProvider(ProviderName).BatchEnd); if (trans == null) { NativeSql.ExecuteNonQuery(sql.ToString()); } else { NativeSql.ExecuteNonQuery(trans, sql.ToString()); } } if (!isSupportBatch && isNeedCommit) { var conn = trans.Connection; trans.Commit(); trans.Dispose(); conn.Close(); conn.Dispose(); } }
/// <summary> /// Betch insert /// </summary> /// <typeparam name="T">Entity</typeparam> /// <param name="list">Entity list</param> /// <param name="isIdAutoGrow">Is id auto grow?</param> /// <param name="trans">Transaction</param> public virtual void Save <T>(List <T> list, bool isIdAutoGrow = true, DbTransaction trans = null) where T : Entity { var sql = new StringBuilder(); var typeName = typeof(T).FullName; var pk = IntrospectionManager.GetPrimaryMemberName(typeName); var setValue = IntrospectionManager.GetSetDelegate(typeName); var isNeedCommit = false; var isSupportBatch = ProviderManager.GetProvider(ProviderName).IsSupportBatch; if (isSupportBatch && ProviderName != DataBaseNames.Oracle) { sql.Append(ProviderManager.GetProvider(ProviderName).BatchBegin); } else if (trans == null) { var conn = NativeSql.GetConn(); conn.Open(); trans = conn.BeginTransaction(); isNeedCommit = true; } foreach (var item in list) { item.Key = Key; var sqlT = item.GetSaveSql(isIdAutoGrow); if (isSupportBatch && ProviderName != DataBaseNames.Oracle) { sql.Append(sqlT); if (isIdAutoGrow) { sql.Append(item.GetLastInsertIdSql()); } } else { if (ProviderName == DataBaseNames.Oracle) { sqlT = sqlT.Remove(sqlT.LastIndexOf(";"), 1); } NativeSql.ExecuteNonQuery(trans, sqlT); if (isIdAutoGrow) { object idValue = NativeSql.ExecuteScalar(trans, item.GetLastInsertIdSql()); if (idValue != null) { var pkType = IntrospectionManager.GetPrimaryKeyType(typeName); setValue(item, pk, idValue.ChangeType(pkType)); } } } } if (isSupportBatch && ProviderName != DataBaseNames.Oracle) { sql.Append(ProviderManager.GetProvider(ProviderName).BatchEnd); DataSet ds; if (trans == null) { ds = NativeSql.ExecuteDataSet(sql.ToString()); } else { ds = NativeSql.ExecuteDataSet(trans, sql.ToString()); } if (isIdAutoGrow) { var pkType = IntrospectionManager.GetPrimaryKeyType(typeName); for (int i = 0; i < list.Count; i++) { object idValue = ds.Tables[i].Rows[0][0]; setValue(list[i], pk, idValue.ChangeType(pkType)); } } } if (!isSupportBatch && isNeedCommit) { var conn = trans.Connection; trans.Commit(); trans.Dispose(); conn.Close(); conn.Dispose(); } }