这一章主要介绍一下如何使用Smark.Data组件进行数据库操作;让使用者了简单了解一下Smark.Data的配置、实体类配置和简单的操作。在使用组件时程序必须引用Smark.Core和Smark.Data两个组件;其源代码可以到:http://smark.codeplex.com/下载。为了方便范例所使用的数据是Northwind.mdb.
l 配置文件设置:
<configSections>
<section name="smarkdata" type="Smark.Data.SmarkDataSection,Smark.Data"/>
</configSections>
<smarkdata>
<Connection>
<add name="0" type="Smark.Data.MSAccess,Smark.Data" connectionstring=""/>
</Connection>
<Assembly>
<add type="Smark.Samples.Entities"/>
</Assembly>
</smarkdata>
l 实体类创建
实体类是通过自定义工具,把相关接口代码生成类代码;所以必须安装Smark.Data.InterfaceToModelGenerator。
创建Entities.cs文件->文件属性的:InterfaceToModel
[Table("Employees")]
interface IEmployee
{
[ID]
[IDENTITY]
int EmployeeID { get; set; }
[Column]
string LastName { get; set; }
[Column]
string FirstName { get; set; }
[Column]
string Title { get; set; }
[Column]
string TitleOfCourtesy { get; set; }
[Column]
DateTime BirthDate { get; set; }
[Column]
DateTime HireDate { get; set; }
[Column]
string Address { get; set; }
[Column]
string City { get; set; }
[Column]
string Region { get; set; }
[Column]
string PostalCode { get; set; }
[Column]
string Country { get; set; }
[Column]
string HomePhone { get; set; }
[Column]
string Extension { get; set; }
[Column]
string Photo { get; set; }
[Column]
string Notes { get; set; }
[Column]
int ReportsTo { get; set; }
}
InterfaceToModelGenerator会根据接口文件生成实体文件
[Table("Employees")]
[Serializable]
public partial class Employee:Smark.Data.Mappings.DataObject,IEmployee
{
[ID]
[IDENTITY]
public int EmployeeID { get{ return mEmployeeID;} set{mEmployeeID=value;EntityState.FieldChange("EmployeeID");} }
private int mEmployeeID;
public static Smark.Data.FieldInfo employeeID = new Smark.Data.FieldInfo("Employees","EmployeeID");
[Column]
public string LastName { get{ return mLastName;} set{mLastName=value;EntityState.FieldChange("LastName");} }
private string mLastName;
public static Smark.Data.FieldInfo lastName = new Smark.Data.FieldInfo("Employees","LastName");
[Column]
public string FirstName { get{ return mFirstName;} set{mFirstName=value;EntityState.FieldChange("FirstName");} }
private string mFirstName;
public static Smark.Data.FieldInfo firstName = new Smark.Data.FieldInfo("Employees","FirstName");
[Column]
public string Title { get{ return mTitle;} set{mTitle=value;EntityState.FieldChange("Title");} }
private string mTitle;
public static Smark.Data.FieldInfo title = new Smark.Data.FieldInfo("Employees","Title");
[Column]
public string TitleOfCourtesy { get{ return mTitleOfCourtesy;} set{mTitleOfCourtesy=value;EntityState.FieldChange("TitleOfCourtesy");} }
private string mTitleOfCourtesy;
public static Smark.Data.FieldInfo titleOfCourtesy = new Smark.Data.FieldInfo("Employees","TitleOfCourtesy");
l 数据库操作
DBContext.SetConnectionString(ConnectionType.Context1, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=http://www.cnblogs.com/Northwind.mdb");
Employee emp = DBContext.Load<Employee>(3);
Console.WriteLine(emp.FirstName + "\t" + emp.LastName);
Console.WriteLine(emp.Country);
emp.Country = emp.Country + "Change";
DBContext.Save(emp);
emp = new Employee();
emp.FirstName = "henry";
emp.LastName = "Fan";
DBContext.Save(emp);
范例详细代码:
http://smark.codeplex.com/源代码的Samples目录下。