Esempio n. 1
0
        /// <summary>
        /// 增加一条数据及其子表
        /// </summary>
        public int Add(Model.channel model)
        {
            //取得站点对应的导航ID
            int parent_id = new DAL.SqlServer.channel_site(databaseprefix).GetSiteNavId(model.site_id);

            if (parent_id == 0)
            {
                return(0);
            }
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into " + databaseprefix + "channel(");
                        strSql.Append("site_id,name,title,is_albums,is_attach,is_spec,sort_id)");
                        strSql.Append(" values (");
                        strSql.Append("@site_id,@name,@title,@is_albums,@is_attach,@is_spec,@sort_id)");
                        strSql.Append(";select @@IDENTITY");
                        SqlParameter[] parameters =
                        {
                            new SqlParameter("@site_id",   SqlDbType.Int,       4),
                            new SqlParameter("@name",      SqlDbType.VarChar,  50),
                            new SqlParameter("@title",     SqlDbType.VarChar, 100),
                            new SqlParameter("@is_albums", SqlDbType.TinyInt,   1),
                            new SqlParameter("@is_attach", SqlDbType.TinyInt,   1),
                            new SqlParameter("@is_spec",   SqlDbType.TinyInt,   1),
                            new SqlParameter("@sort_id",   SqlDbType.Int, 4)
                        };
                        parameters[0].Value = model.site_id;
                        parameters[1].Value = model.name;
                        parameters[2].Value = model.title;
                        parameters[3].Value = model.is_albums;
                        parameters[4].Value = model.is_attach;
                        parameters[5].Value = model.is_spec;
                        parameters[6].Value = model.sort_id;
                        object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务
                        model.id = Convert.ToInt32(obj);

                        //扩展字段
                        if (model.channel_fields != null)
                        {
                            StringBuilder strSql2;
                            foreach (Model.channel_field modelt in model.channel_fields)
                            {
                                strSql2 = new StringBuilder();
                                strSql2.Append("insert into " + databaseprefix + "channel_field(");
                                strSql2.Append("channel_id,field_id)");
                                strSql2.Append(" values (");
                                strSql2.Append("@channel_id,@field_id)");
                                SqlParameter[] parameters2 =
                                {
                                    new SqlParameter("@channel_id", SqlDbType.Int, 4),
                                    new SqlParameter("@field_id",   SqlDbType.Int, 4)
                                };
                                parameters2[0].Value = model.id;
                                parameters2[1].Value = modelt.field_id;
                                DbHelperSQL.ExecuteSql(conn, trans, strSql2.ToString(), parameters2);
                            }
                        }

                        //添加视图
                        StringBuilder strSql3 = new StringBuilder();
                        strSql3.Append("CREATE VIEW view_channel_" + model.name + " as");
                        strSql3.Append(" SELECT " + databaseprefix + "article.*");
                        if (model.channel_fields != null)
                        {
                            foreach (Model.channel_field modelt in model.channel_fields)
                            {
                                Model.article_attribute_field fieldModel = new DAL.SqlServer.article_attribute_field(databaseprefix).GetModel(modelt.field_id);
                                if (fieldModel != null)
                                {
                                    strSql3.Append("," + databaseprefix + "article_attribute_value." + fieldModel.name);
                                }
                            }
                        }
                        strSql3.Append(" FROM " + databaseprefix + "article_attribute_value INNER JOIN");
                        strSql3.Append(" " + databaseprefix + "article ON " + databaseprefix + "article_attribute_value.article_id = " + databaseprefix + "article.id");
                        strSql3.Append(" WHERE " + databaseprefix + "article.channel_id=" + model.id);
                        DbHelperSQL.ExecuteSql(conn, trans, strSql3.ToString());

                        //添加导航菜单
                        int newNavId = new DAL.SqlServer.navigation(databaseprefix).Add(conn, trans, parent_id, "channel_" + model.name, model.title, "", model.sort_id, model.id, "Show");
                        new DAL.SqlServer.navigation(databaseprefix).Add(conn, trans, newNavId, "channel_" + model.name + "_list", "内容管理", "article/article_list.aspx", 99, model.id, "Show,View,Add,Edit,Delete,Audit");
                        new DAL.SqlServer.navigation(databaseprefix).Add(conn, trans, newNavId, "channel_" + model.name + "_category", "栏目类别", "article/category_list.aspx", 100, model.id, "Show,View,Add,Edit,Delete");
                        new DAL.SqlServer.navigation(databaseprefix).Add(conn, trans, newNavId, "channel_" + model.name + "_comment", "评论管理", "article/comment_list.aspx", 101, model.id, "Show,View,Delete,Reply");

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return(0);
                    }
                }
            }
            return(model.id);
        }
Esempio n. 2
0
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.channel model)
        {
            Model.channel oldModel = GetModel(model.id); //旧的数据
            //取得站点对应的导航ID
            int parent_id = new DAL.SqlServer.channel_site(databaseprefix).GetSiteNavId(model.site_id);

            if (parent_id == 0)
            {
                return(false);
            }
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update " + databaseprefix + "channel set ");
                        strSql.Append("site_id=@site_id,");
                        strSql.Append("name=@name,");
                        strSql.Append("title=@title,");
                        strSql.Append("is_albums=@is_albums,");
                        strSql.Append("is_attach=@is_attach,");
                        strSql.Append("is_spec=@is_spec,");
                        strSql.Append("sort_id=@sort_id");
                        strSql.Append(" where id=@id ");
                        SqlParameter[] parameters =
                        {
                            new SqlParameter("@site_id",   SqlDbType.Int,       4),
                            new SqlParameter("@name",      SqlDbType.VarChar,  50),
                            new SqlParameter("@title",     SqlDbType.VarChar, 100),
                            new SqlParameter("@is_albums", SqlDbType.TinyInt,   1),
                            new SqlParameter("@is_attach", SqlDbType.TinyInt,   1),
                            new SqlParameter("@is_spec",   SqlDbType.TinyInt,   1),
                            new SqlParameter("@sort_id",   SqlDbType.Int,       4),
                            new SqlParameter("@id",        SqlDbType.Int, 4)
                        };
                        parameters[0].Value = model.site_id;
                        parameters[1].Value = model.name;
                        parameters[2].Value = model.title;
                        parameters[3].Value = model.is_albums;
                        parameters[4].Value = model.is_attach;
                        parameters[5].Value = model.is_spec;
                        parameters[6].Value = model.sort_id;
                        parameters[7].Value = model.id;
                        DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);

                        //删除已移除的扩展字段
                        FieldDelete(conn, trans, model.channel_fields, model.id);
                        //添加扩展字段
                        if (model.channel_fields != null)
                        {
                            StringBuilder strSql2;
                            foreach (Model.channel_field modelt in model.channel_fields)
                            {
                                strSql2 = new StringBuilder();
                                Model.channel_field fieldModel = null;
                                if (oldModel.channel_fields != null)
                                {
                                    fieldModel = oldModel.channel_fields.Find(p => p.field_id == modelt.field_id); //查找是否已经存在
                                }
                                if (fieldModel == null)                                                            //如果不存在则添加
                                {
                                    strSql2.Append("insert into " + databaseprefix + "channel_field(");
                                    strSql2.Append("channel_id,field_id)");
                                    strSql2.Append(" values (");
                                    strSql2.Append("@channel_id,@field_id)");
                                    SqlParameter[] parameters2 =
                                    {
                                        new SqlParameter("@channel_id", SqlDbType.Int, 4),
                                        new SqlParameter("@field_id",   SqlDbType.Int, 4)
                                    };
                                    parameters2[0].Value = modelt.channel_id;
                                    parameters2[1].Value = modelt.field_id;
                                    DbHelperSQL.ExecuteSql(conn, trans, strSql2.ToString(), parameters2);
                                }
                            }
                        }
                        //删除旧视图重建新视图
                        RehabChannelViews(conn, trans, model, oldModel.name);

                        //修改对应的导航
                        new DAL.SqlServer.navigation(databaseprefix).Update(conn, trans, "channel_" + oldModel.name, parent_id, "channel_" + model.name, model.title, model.sort_id);
                        new DAL.SqlServer.navigation(databaseprefix).Update(conn, trans, "channel_" + oldModel.name + "_list", "channel_" + model.name + "_list");         //内容管理
                        new DAL.SqlServer.navigation(databaseprefix).Update(conn, trans, "channel_" + oldModel.name + "_category", "channel_" + model.name + "_category"); //栏目类别
                        new DAL.SqlServer.navigation(databaseprefix).Update(conn, trans, "channel_" + oldModel.name + "_comment", "channel_" + model.name + "_comment");   //评论管理

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return(false);
                    }
                }
            }
            return(true);
        }