数据的增删改查(三层)<!--待补充-->

      进行数据操作必然少了对数据的增删改查,用代码生成器生成的代码不是那么满意!方便在今后使用,这里就主要写“数据访问层(Dal)”

既然这里提到三层架构:有必要将三层内容在这里详细介绍一下(待补充)

注:这里进行数据库操作时,进行参数化查询语句就是我不太推荐的一种写法!

参数化查询推荐写法见:http://www.cnblogs.com/fengxuehuanlin/p/5362697.html

 

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long Id)
        {
            string sql = "select count(*) Name from t_temp where ID=@id";
            MySqlParameter[] param = { new MySqlParameter("@id", Id) };
            long data=(long)MySqlHelper.ExecuteScalar(sql, param);
            if (data > 0)
            {
                return true;
            }        
            return false;
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(Model model)
        {
            string sql = "insert into t_temp (Name) values (@name)";
            MySqlParameter[] param = { new MySqlParameter("@name", model.Name) };
            return MySqlHelper.ExecuteNonQuery(sql, param);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model model)
        {
            string sql = "Update t_temp set Name=@name where ID=@id";
            MySqlParameter[] param = { new MySqlParameter("@name", model.Name), new MySqlParameter("@id", model.Id) };
            if (MySqlHelper.ExecuteNonQuery(sql, param) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long Id)
        {
            string sql = "delete from t_temp where ID=@id";
            MySqlParameter[] param = { new MySqlParameter("@id", Id) };
            if (MySqlHelper.ExecuteNonQuery(sql, param) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            string sql = "delete from t_temp where ID in (" + Idlist + ")";
            if (MySqlHelper.ExecuteNonQuery(sql) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model GetModel(long Id)
        {
            string sql = "select ID,Name from t_temp where ID=@id";
            MySqlParameter[] param = { new MySqlParameter("@id", Id) };
            DataTable table= MySqlHelper.ExecuteQuery(sql, param);
            if (table != null)
            {
                if (table.Rows.Count > 0)
                {
                    return DataRowToModel(table.Rows[0]);
                }
            }
            return null;          
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model DataRowToModel(DataRow row)
        {
            Model mod = new Model();
            if (row != null)
            {
                if(!string.IsNullOrEmpty(row["ID"].ToString()))
                {
                    mod.Id=long.Parse(row["ID"].ToString());
                }
                if (!string.IsNullOrEmpty(row["Name"].ToString()))
                {
                    mod.Name = row["Name"].ToString();
                }
            }
            return mod;
        }

 

 

 

posted on 2016-03-13 23:22  风雪幻林  阅读(398)  评论(0编辑  收藏  举报