Model
数据库模型,封装数据库中的表
例如:Person表,
代码
/// <summary>
/// 实体类Person 。(属性说明自动提取数据库字段的描述信息)
/// </summary>
[Serializable]
public class Person
{
public Person()
{}
#region Model
private int _personid;
private string _lastname;
private string _firstname;
private int _personcategory;
private DateTime? _hiredate;
private DateTime? _enrollmentdate;
private byte[] _picture;
private string password;
/// <summary>
///
/// </summary>
public int PersonID
{
set{ _personid=value;}
get{return _personid;}
}
/// <summary>
///
/// </summary>
public string LastName
{
set{ _lastname=value;}
get{return _lastname;}
}
/// <summary>
///
/// </summary>
public string FirstName
{
set{ _firstname=value;}
get{return _firstname;}
}
/// <summary>
///
/// </summary>
public int PersonCategory
{
set{ _personcategory=value;}
get{return _personcategory;}
}
/// <summary>
///
/// </summary>
public DateTime? HireDate
{
set{ _hiredate=value;}
get{return _hiredate;}
}
/// <summary>
///
/// </summary>
public DateTime? EnrollmentDate
{
set{ _enrollmentdate=value;}
get{return _enrollmentdate;}
}
/// <summary>
///
/// </summary>
public byte[] Picture
{
set{ _picture=value;}
get{return _picture;}
}
public string Password
{
get { return password; }
set { password=value;}
}
#endregion Model
}
/// 实体类Person 。(属性说明自动提取数据库字段的描述信息)
/// </summary>
[Serializable]
public class Person
{
public Person()
{}
#region Model
private int _personid;
private string _lastname;
private string _firstname;
private int _personcategory;
private DateTime? _hiredate;
private DateTime? _enrollmentdate;
private byte[] _picture;
private string password;
/// <summary>
///
/// </summary>
public int PersonID
{
set{ _personid=value;}
get{return _personid;}
}
/// <summary>
///
/// </summary>
public string LastName
{
set{ _lastname=value;}
get{return _lastname;}
}
/// <summary>
///
/// </summary>
public string FirstName
{
set{ _firstname=value;}
get{return _firstname;}
}
/// <summary>
///
/// </summary>
public int PersonCategory
{
set{ _personcategory=value;}
get{return _personcategory;}
}
/// <summary>
///
/// </summary>
public DateTime? HireDate
{
set{ _hiredate=value;}
get{return _hiredate;}
}
/// <summary>
///
/// </summary>
public DateTime? EnrollmentDate
{
set{ _enrollmentdate=value;}
get{return _enrollmentdate;}
}
/// <summary>
///
/// </summary>
public byte[] Picture
{
set{ _picture=value;}
get{return _picture;}
}
public string Password
{
get { return password; }
set { password=value;}
}
#endregion Model
}
DBAccess
数据库访问层,用ADO.NET 提供访问数据库的借口,包括各种数据库的访问接口:,使用了简单工厂设计模式。
接口:ISQLHelper
代码
SQLServer帮助类:DbHelperSQL
代码
创建帮助类的工厂:SQLHelperFactory,根据配置文件,初始化不同的数据库帮助类
代码
/// <summary>
/// 创建帮助类的工厂
/// </summary>
public class SQLHelperFactory
{
public static ISQLHelper GetHelper()
{
switch(PubConstant.DBTypeString)
{
case "MSSQL":
return new DbHelperSQL(PubConstant.ConnectionString);
case "Oracle":
return new DbHelperOra(PubConstant.ConnectionString);
case "Oledb":
return new DbHelperOleDb(PubConstant.ConnectionString);
default:
throw new Exception("请确保配置文件的真确性!");
}
}
}
/// 创建帮助类的工厂
/// </summary>
public class SQLHelperFactory
{
public static ISQLHelper GetHelper()
{
switch(PubConstant.DBTypeString)
{
case "MSSQL":
return new DbHelperSQL(PubConstant.ConnectionString);
case "Oracle":
return new DbHelperOra(PubConstant.ConnectionString);
case "Oledb":
return new DbHelperOleDb(PubConstant.ConnectionString);
default:
throw new Exception("请确保配置文件的真确性!");
}
}
}
DataAccessLayer
具体对象的数据库访问抽象类,基础类
代码
public class DataAccessBase
{
protected ISQLHelper DbHelperSQL
{
get
{
return SQLHelperFactory.GetHelper();
}
}
public DataAccessBase()
{ }
}
{
protected ISQLHelper DbHelperSQL
{
get
{
return SQLHelperFactory.GetHelper();
}
}
public DataAccessBase()
{ }
}
Person对象数据库访问类
代码
/// <summary>
/// 数据访问类PersonDataAccess
/// </summary>
public class PersonDataAccess:DataAccessBase
{
public PersonDataAccess()
{}
#region 成员方法
/// <summary>
/// 得到最大ID
/// </summary>
public int GetMaxId()
{
return DbHelperSQL.GetMaxID("PersonID", "Person");
}
/// 数据访问类PersonDataAccess
/// </summary>
public class PersonDataAccess:DataAccessBase
{
public PersonDataAccess()
{}
#region 成员方法
/// <summary>
/// 得到最大ID
/// </summary>
public int GetMaxId()
{
return DbHelperSQL.GetMaxID("PersonID", "Person");
}
BussinessIntegrateLayer
逻辑访问层,即对外界的访问接口,外部直接此组件中的Service方法就可以了。
代码
以上只是部分代码,源码如下,并附有数据库建库脚本,希望广大网友斧正。