示例#1
0
        private void InsertSubCategories()
        {
            DbTableMap subCategoryMap = Config.GetMap(typeof(FeatureSubCategory));
            Dictionary<string, List<Pair<string, object>>> subCategoryDict = new Dictionary<string, List<Pair<string, object>>>();

            foreach (DbFeatureCategory category in Config.FeatureCategories)
            {
                foreach (DbFeatureSubCategory subCategory in category.SubCategories)
                {
                    string key = string.Format("{0}-{1}", category.Id, subCategory.Id);

                    subCategoryDict.Add(key, new List<Pair<string, object>>());

                    string idColumn = subCategoryMap.GetIdColumnName();
                    string fkColumn = subCategoryMap.GetForeignKeyColumn(typeof(FeatureCategory));
                    string niceNameColumn = subCategoryMap.GetColumnForMemberName("NiceName");

                    subCategoryDict[key].Add(new Pair<string, object>(idColumn, subCategory.Id));
                    subCategoryDict[key].Add(new Pair<string, object>(fkColumn, category.Id));
                    subCategoryDict[key].Add(new Pair<string, object>(niceNameColumn, subCategory.NiceName));
                }
            }

            DbConnection connection = null;
            DbTransaction ta = null;

            try
            {
                connection = OpenConnection();
                ta = BeginTransaction(connection);

                foreach (List<Pair<string, object>> pairs in subCategoryDict.Values)
                {
                    QueryBuilder builder = new QueryBuilder(QueryType.Insert);
                    string sql = builder.CreateInsertSql(subCategoryMap.Table, pairs);

                    ExecuteNonQuery(sql, connection);
                }

                Commit(ta);
            }
            catch (Exception)
            {
                if (ta != null)
                {
                    Rollback(ta);
                }
                throw;
            }
            finally
            {
                if (connection != null)
                {
                    CloseConnection(connection);
                }
            }
        }
示例#2
0
        private void InsertCategories()
        {
            DbTableMap categoryMap = Config.GetMap(typeof(FeatureCategory));

            Dictionary<int, List<Pair<string, object>>> categoryDict = new Dictionary<int, List<Pair<string, object>>>();

            foreach (DbFeatureCategory category in Config.FeatureCategories)
            {
                categoryDict.Add(category.Id, new List<Pair<string, object>>());

                string idColumn = categoryMap.GetIdColumnName();
                string nicenameColumn = categoryMap.GetColumnForMemberName("NiceName");

                categoryDict[category.Id].Add(new Pair<string, object>(idColumn, category.Id));
                categoryDict[category.Id].Add(new Pair<string, object>(nicenameColumn, category.NiceName));
            }

            DbConnection connection = null;
            DbTransaction ta = null;

            try
            {
                connection = OpenConnection();
                ta = BeginTransaction(connection);

                foreach (List<Pair<string, object>> pairs in categoryDict.Values)
                {
                    QueryBuilder builder = new QueryBuilder(QueryType.Insert);
                    string sql = builder.CreateInsertSql(categoryMap.Table, pairs);

                    ExecuteNonQuery(sql, connection);
                }

                Commit(ta);
            }
            catch (Exception)
            {
                if (ta != null)
                {
                    ta.Rollback();
                }
                throw;
            }
            finally
            {
                if (connection != null)
                {
                    CloseConnection(connection);
                }
            }
        }