Beispiel #1
0
        private static int Count( String condition, EntityInfo entityInfo ) {
            String countSql;
            int result = 0;
            SqlBuilder builder = new SqlBuilder( entityInfo );
            if (strUtil.IsNullOrEmpty( condition )) {
                countSql = String.Format( "select count(*) from {0}", entityInfo.TableName );
            }
            else {
                countSql = builder.GetCountSql( condition );
            }
            logger.Info(LoggerUtil.SqlPrefix + "[Count(String condition) Sql]:" + countSql);
            var conn = DbContext.getConnection(entityInfo);
            if (conn.State == System.Data.ConnectionState.Closed)
            {
                conn.Open();
                OrmHelper.initCount++;
                LogManager.GetLogger("Class:System.ORM.Operation.CountOperation Method:Count").Info("数据库连接已开启【" + OrmHelper.initCount + "】");
            }
            try
            {
                IDbCommand command = DataFactory.GetCommand(countSql, conn);
                result = cvt.ToInt( command.ExecuteScalar() );
            }
            catch (Exception exception) {
                logger.Error( exception.Message );
                throw exception;
            }
            finally
            {
                if (!DbContext.shouldTransaction())
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                        OrmHelper.clostCount++;
                        LogManager.GetLogger("Class:System.ORM.Operation.CountOperation Method:Count").Info("数据库连接已关闭【" + OrmHelper.clostCount + "】");
                    }
                }
            }

            return result;
        }
        private static int Count( String condition, EntityInfo entityInfo )
        {
            String countSql;
            int result = 0;
            SqlBuilder builder = new SqlBuilder( entityInfo );
            if (strUtil.IsNullOrEmpty( condition )) {
                countSql = String.Format( "select count(*) from {0}", entityInfo.TableName );
            }
            else {
                countSql = builder.GetCountSql( condition );
            }
            logger.Info( LoggerUtil.SqlPrefix + "[Count(String condition) Sql]:" + countSql );
            IDbCommand command = DataFactory.GetCommand( countSql, DbContext.getConnection( entityInfo ) );
            try {
                result = cvt.ToInt( command.ExecuteScalar() );
            }
            catch (Exception exception) {
                logger.Error( exception.Message );
                throw new OrmException( exception.Message, exception );
            }

            return result;
        }