DbEntry 简单实现

在着手编码之前首先安装DbEntry DbEntry.Net.4.1.Setup.zip

在建立类库时选择 DbEntryClassLibrary 如图

 DbEntryClassLibrary1 中建立 实体和数据库访问类

如下

 1  public class Company :DbObjectModel<Company,int>
 2     {
 3         public string CompanyName { get; set; }
 4         public string CompanyID { get; set; }
 5         public int CompanyType { get; set; }
 6     }
 7 
 8     public class CompanyDao
 9     {
10         public IList<Company> GetBetweenCompanys()
11         {
12             IList<Company> BetweenCompanys = DbEntry.From<Company>().Where(o=>o.Id>0).Select();
13             return BetweenCompanys;
14         }
15 
16         public Company GetCompany(int id)
17         {
18             return DbEntry.GetObject<Company>(id);
19         }
20 
21         public void UpdateCompany(Company c)
22         {
23             DbEntry.Update(c);
24         }
25 
26         public void RemoveCompany(Company c)
27         {
28             DbEntry.Delete(c);
29         }
30 
31         public void InsertCompany(Company c)
32         {
33             DbEntry.Insert(c);
34         }
35     }
View Code

客户端调用

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             CompanyDao dao = new CompanyDao();
 6             //获取列表
 7             IList<Company> list = dao.GetBetweenCompanys();
 8 
 9             //查询ID为1的Compny
10             int id = 1;
11             Company company = dao.GetCompany(id);
12 
13             company.CompanyType = 1;//修改Compny
14             dao.UpdateCompany(company);
15 
16             //添加
17             Company cp = new Company();
18             cp.CompanyID = "201306271539";
19             cp.CompanyName = "InsertTest";
20             cp.CompanyType = 0;
21 
22             dao.InsertCompany(cp);
23 
24             //删除
25             Company c = new Company();
26             c.Id = 2;
27             dao.RemoveCompany(c);
28         }
29     }
View Code

App.config配置

1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3   <configSections>
4     <section name="Lephone.Settings" type="Lephone.Core.Setting.NameValueSectionHandler, Lephone.Core" />
5   </configSections>
6   <Lephone.Settings>
7     <add key="DataBase" value="@SqlServer2005 :Data Source=127.0.0.1;initial catalog=TestDb;user id=sa;password=123" />
8   </Lephone.Settings>
9 </configuration>
View Code

数据库Company表

原文地址:https://www.cnblogs.com/yf2011/p/3158984.html