Beispiel #1
0
        /// <summary>
        /// 将对象转换实体
        /// </summary>
        public Dapper.Model.site_channel DataRowToModel(DataRow row)
        {
            Dapper.Model.site_channel model = new Dapper.Model.site_channel();
            if (row != null)
            {
                #region 主表信息======================
                //利用反射获得属性的所有公共属性
                Type modelType = model.GetType();
                for (int i = 0; i < row.Table.Columns.Count; i++)
                {
                    PropertyInfo proInfo = modelType.GetProperty(row.Table.Columns[i].ColumnName);
                    if (proInfo != null && row[i] != DBNull.Value)
                    {
                        proInfo.SetValue(model, row[i], null);//用索引值设置属性值
                    }
                }
                #endregion

                #region 子表信息======================
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select * from " + databaseprefix + "site_channel_field");
                strSql.Append(" where channel_id=@channel_id");
                SqlParameter[] parameters =
                {
                    new SqlParameter("@channel_id", SqlDbType.Int, 4)
                };
                parameters[0].Value = model.id;
                DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    int rowsCount = dt.Rows.Count;
                    List <Dapper.Model.site_channel_field> models = new List <Dapper.Model.site_channel_field>();
                    Dapper.Model.site_channel_field        modelt;
                    for (int n = 0; n < rowsCount; n++)
                    {
                        modelt = new Dapper.Model.site_channel_field();
                        Type modeltType = modelt.GetType();
                        for (int i = 0; i < dt.Rows[n].Table.Columns.Count; i++)
                        {
                            PropertyInfo proInfo = modeltType.GetProperty(dt.Rows[n].Table.Columns[i].ColumnName);
                            if (proInfo != null && dt.Rows[n][i] != DBNull.Value)
                            {
                                proInfo.SetValue(modelt, dt.Rows[n][i], null);
                            }
                        }
                        models.Add(modelt);
                    }
                    model.channel_fields = models;
                }
                #endregion
            }
            return(model);
        }
Beispiel #2
0
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Dapper.Model.site_channel GetModel(string channel_name)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder str1   = new StringBuilder();

            Dapper.Model.site_channel model = new Dapper.Model.site_channel();
            //利用反射获得属性的所有公共属性
            PropertyInfo[] pros = model.GetType().GetProperties();
            foreach (PropertyInfo p in pros)
            {
                //拼接字段,忽略List<T>
                if (!typeof(System.Collections.IList).IsAssignableFrom(p.PropertyType))
                {
                    str1.Append(p.Name + ",");
                }
            }
            strSql.Append("select top 1 " + str1.ToString().Trim(','));
            strSql.Append(" from " + databaseprefix + "site_channel");
            strSql.Append(" where name=@channel_name ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@channel_name", SqlDbType.VarChar, 50)
            };
            parameters[0].Value = channel_name;

            DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];

            if (dt.Rows.Count > 0)
            {
                return(DataRowToModel(dt.Rows[0]));
            }
            else
            {
                return(null);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 增加一条数据及其子表
        /// </summary>
        public int Add(Dapper.Model.site_channel model)
        {
            //取得站点对应的导航,如果站点导航不存在则退出新增
            int parent_id = new DAL.sites(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
                    {
                        #region 写入频道表数据==================
                        StringBuilder strSql = new StringBuilder();
                        StringBuilder str1   = new StringBuilder(); //数据字段
                        StringBuilder str2   = new StringBuilder(); //数据参数
                        //利用反射获得属性的所有公共属性
                        PropertyInfo[]      pros  = model.GetType().GetProperties();
                        List <SqlParameter> paras = new List <SqlParameter>();
                        strSql.Append("insert into " + databaseprefix + "site_channel(");
                        foreach (PropertyInfo pi in pros)
                        {
                            //如果不是主键和List<T>类型则追加sql字符串
                            if (!pi.Name.Equals("id") && !typeof(System.Collections.IList).IsAssignableFrom(pi.PropertyType))
                            {
                                //判断属性值是否为空
                                if (pi.GetValue(model, null) != null)
                                {
                                    str1.Append(pi.Name + ",");                                           //拼接字段
                                    str2.Append("@" + pi.Name + ",");                                     //声明参数
                                    paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值
                                }
                            }
                        }
                        strSql.Append(str1.ToString().Trim(','));
                        strSql.Append(") values (");
                        strSql.Append(str2.ToString().Trim(','));
                        strSql.Append(") ");
                        strSql.Append(";select @@IDENTITY;");
                        object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), paras.ToArray());//带事务
                        model.id = Convert.ToInt32(obj);
                        #endregion

                        //写入扩展字段及创建频道数据表
                        FieldAdd(conn, trans, model);

                        #region 写入导航数据===============
                        int newNavId = new DAL.navigation(databaseprefix).Add(conn, trans, parent_id, "channel_" + model.name, model.title, "", model.sort_id, model.id, "Show");
                        new DAL.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.navigation(databaseprefix).Add(conn, trans, newNavId, "channel_" + model.name + "_category", "栏目类别", "article/category_list.aspx", 100, model.id, "Show,View,Add,Edit,Delete");
                        //开启评论则新增菜单
                        if (model.is_comment > 0)
                        {
                            new DAL.navigation(databaseprefix).Add(conn, trans, newNavId, "channel_" + model.name + "_comment", "评论管理", "article/comment_list.aspx", 103, model.id, "Show,View,Delete,Reply");
                        }
                        #endregion

                        trans.Commit();//提交事务
                    }
                    catch
                    {
                        trans.Rollback();//回滚事务
                        return(0);
                    }
                }
            }
            return(model.id);
        }
Beispiel #4
0
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Dapper.Model.site_channel model)
        {
            Dapper.Model.site_channel oldModel = GetModel(model.id);                         //取得旧的频道数据
            int siteNavParentId = new DAL.sites(databaseprefix).GetSiteNavId(model.site_id); //取得站点对应的导航ID

            if (siteNavParentId == 0)
            {
                return(false);
            }
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 修改频道表======================
                        StringBuilder strSql = new StringBuilder();
                        StringBuilder str1   = new StringBuilder();
                        //利用反射获得属性的所有公共属性
                        PropertyInfo[]      pros  = model.GetType().GetProperties();
                        List <SqlParameter> paras = new List <SqlParameter>();
                        strSql.Append("update  " + databaseprefix + "site_channel set ");
                        foreach (PropertyInfo pi in pros)
                        {
                            //如果不是主键则追加sql字符串
                            //!pi.Name.Equals("channel_fields")
                            if (!pi.Name.Equals("id") && !typeof(System.Collections.IList).IsAssignableFrom(pi.PropertyType))
                            {
                                //判断属性值是否为空
                                if (pi.GetValue(model, null) != null)
                                {
                                    str1.Append(pi.Name + "=@" + pi.Name + ",");                          //声明参数
                                    paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值
                                }
                            }
                        }
                        strSql.Append(str1.ToString().Trim(','));
                        strSql.Append(" where id=@id ");
                        paras.Add(new SqlParameter("@id", model.id));
                        DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), paras.ToArray());
                        #endregion

                        //删除已移除扩展字段及频道数据表列
                        FieldDelete(conn, trans, model, oldModel);
                        //编辑扩展字段及频道数据表
                        FieldUpdate(conn, trans, model, oldModel);

                        #region 编辑对应的导航==================
                        new DAL.navigation(databaseprefix).Update(conn, trans, "channel_" + oldModel.name, siteNavParentId, "channel_" + model.name, model.title, model.sort_id);
                        new DAL.navigation(databaseprefix).Update(conn, trans, "channel_" + oldModel.name + "_list", "channel_" + model.name + "_list");         //内容管理
                        new DAL.navigation(databaseprefix).Update(conn, trans, "channel_" + oldModel.name + "_category", "channel_" + model.name + "_category"); //栏目类别

                        //评论导航菜单处理
                        if (model.is_comment > 0 && oldModel.is_comment == 0)
                        {
                            //如开启评论而以前关闭的需新增菜单
                            new DAL.navigation(databaseprefix).Add(conn, trans, "channel_" + model.name, "channel_" + model.name + "_comment", "评论管理", "article/comment_list.aspx", 103, model.id, "Show,View,Add,Edit,Delete");
                        }
                        else if (model.is_comment == 0 && oldModel.is_comment > 0)
                        {
                            //如关闭评论而以前开启需删除菜单
                            new DAL.navigation(databaseprefix).Delete(conn, trans, "channel_" + oldModel.name + "_comment");
                        }
                        else if (model.is_comment > 0)
                        {
                            //如都是开启状态则修改菜单
                            new DAL.navigation(databaseprefix).Update(conn, trans, "channel_" + oldModel.name + "_comment", "channel_" + model.name + "_comment");
                        }
                        #endregion

                        trans.Commit();//提交事务
                    }
                    catch
                    {
                        trans.Rollback();//回滚事务
                        return(false);
                    }
                }
            }
            return(true);
        }