具体的数据库操作类,Model类和DB类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DBModels
{
    public class StudentModel
    {
        public StudentModel()
        { }
        #region property
        public string StudentID
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public string Age
        {
            get;
            set;
        }

        public string BirthDay
        {
            get;
            set;
        }

        #endregion
    }
}

Model层,其中这些属性都是根据数据库的字段对应的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBModels;

namespace DBAccess
{
    public class StudentDB : BaseOperation
    {
        private static readonly string[] _nameString = new string[] { "StudentID", "Name", "Age", "BirthDay" };
        private static readonly string[] _nameStringType = new string[] { "int", "string", "float", "datetime" };
        private static readonly string tableName = "Student";
        public StudentDB()
            : base()
        { }
        private void FillList(ref List<string> tempList, StudentModel model)
        {
            tempList.Add(model.StudentID);
            tempList.Add(model.Name);
            tempList.Add(model.Age);
            tempList.Add(model.BirthDay);
        }
        private List<StudentModel> ModelBind(DataTable dt)
        {
            List<StudentModel> models = new List<StudentModel>();
            StudentModel model = null;
            if (dt != null && dt.Rows.Count != 0)
            {
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    model = new StudentModel();
                    model.StudentID = dt.Rows[i]["StudentID"].ToString();
                    model.Name = dt.Rows[i]["Name"].ToString();
                    model.Age = dt.Rows[i]["Age"].ToString();
                    model.BirthDay = dt.Rows[i]["BirthDay"].ToString();

                    models.Add(model);
                }
            }
            return models;
        }

        /// <summary>
        /// 添加模型及相应的动作
        /// </summary>
        /// <param name="model">模型</param>
        /// <param name="dbo">具体动作</param>
        public void AddAction(StudentModel model, DBOperation dbo)
        {
            List<string> paramValues = new List<string>();
            FillList(ref paramValues, model);
            AddAction(paramValues, tableName, _nameString, _nameStringType, dbo);
        }

        /// <summary>
        /// 数据插入表Item中,返回插入值的ID号
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(StudentModel model)
        {
            List<string> paramValues = new List<string>();
            FillList(ref paramValues, model);
            return BaseInsertItem(paramValues, tableName, _nameString, _nameStringType);
        }

        /// <summary>
        /// 根据条件删除Items的数据
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns></returns>
        public int Delete(string strWhere)
        {
            return BaseDelete(tableName, strWhere);
        }

        /// <summary>
        /// 根据条件更新Items的数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public int Update(StudentModel model)
        {
            List<string> paramValues = new List<string>();
            FillList(ref paramValues, model);
            return BaseUpdateItem(paramValues, tableName, _nameString, _nameStringType);
        }

        /// <summary>
        /// 根据条件选择Items的数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<StudentModel> GetModels(string strWhere)
        {
            DataTable dt = BaseSelectItem(tableName, _nameString, strWhere);
            List<StudentModel> _tempModel = ModelBind(dt);
            return _tempModel;
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="strWhere">条件语句</param>
        /// <returns>存在或者不存在(true,false)</returns>
        public bool Exists(string strWhere)
        {
            if (strWhere != null && strWhere.Trim().Length > 0)
            {
                return BaseExists(string.Format("select count(0) from {0} where {1}", tableName, strWhere));
            }
            else
            {
                return BaseExists(string.Format("select count(0) from {0}", tableName));
            }
        }

        /// <summary>
        /// 获取主键最大值
        /// </summary>
        /// <returns>主键最大值</returns>
        public int GetMaxId()
        {
            return BaseGetMaxId(string.Format("select Max({0}) from {1}", _nameString[0], tableName));
        }
    }
}


自己还制作了一个代码生成器,底层的类因为都是统一的,只需要根据数据库的字段修改Model层和DB层就可以了

另外,还能对该模型进行事务的批处理,先调用基类的StartMultiOperation,再调用AddAction,添加具体动作,最后执行ExecuteMultiOperation即可


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值