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即可