在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:easy4net开源软件地址:https://gitee.com/wangwei123/easy4net开源软件介绍:easy4net是一个轻量级orm框架,灵活在于可以自己编写复杂的SQL语句查询,简单在于几分钟内便能上手使用,并支持mysql, mssql, oracle, access, sqlite数据库. easy4net技术QQ 群:162695864 分页查询:
int pageIndex = 1;int pageSize = 3;string strSql = "SELECT e.*, c.company_name FROM employee e INNER JOIN company c ON e.company_id = c.id WHERE e.name = @name";ParamMap param = ParamMap.newMap();param.setPageParamters(page, limit);//分页时使用的排序字段,必填,请带上SQL表名的别名,如employee的为: eparam.setOrderFields("e.id", true);param.setParameter("name", "LiYang");//MySQLString是app.config或web.config中配置的连接数据库字符串,//应部分用户需要可以切换多个数据库要求Session session = SessionFactory.GetSession("MySQLString");List<Employee> emList = session.Find<Employee>(strSql, param); 分页查询:
public PageResult<Store> findByPage(int page, int limit){ Session session = SessionFactory.GetSession("MySQLString"); String sql = "select * from store"; ParamMap param = ParamMap.newMap(); param.setPageParamters(page, limit); param.setOrderFields("id", true); PageResult<Store> pageResult = session.FindPage<Store>(sql, param); return pageResult;}public class PageResult<T>{ /// <summary> /// 分页查询中总记录数 /// </summary> public int Total {get; set;} /// <summary> /// 分页查询中结果集合 /// </summary> public List<T> DataList {get; set;}} 查询单条记录: string strSql = "SELECT e.*, c.company_name FROM employee e INNER JOIN company c ON e.company_id = c.id WHERE e.name = @name";ParamMap param = ParamMap.newMap();param.setParameter("name", "LiYang");Employee em = session.Get<Employee>(strSql, param); 普通查询: string strSql = "SELECT e.*, c.company_name FROM employee e INNER JOIN company c ON e.company_id = c.id";List<Employee> emList = session.Find<Employee>(strSql); 新增:
//创建公司Company company = new Company();company.CompanyName = txtName.Text.Trim();company.Industry = txtIndustry.Text.Trim();company.Address = txtAddress.Text.Trim();Session session = SessionFactory.GetSession("MySQLString");session.Insert<Company>(company);if (company.Id > 0) { MessageBox.Show("创建公司成功!");}//新增员工Company company = m_CompanyList[cbCompany.SelectedIndex]; Employee employee = new Employee();employee.Name = txtName.Text.Trim();mployee.Age = Convert.ToInt32(txtAge.Text.Trim());employee.Address = txtAddress.Text.Trim();employee.Created = DateTime.Now;employee.CompanyId = company.Id;Session session = SessionFactory.GetSession("MySQLString");session.Insert<Employee>(employee);if (employee.Id > 0){ MessageBox.Show("新增员工成功!");} 批量新增:
List<Company> companyList = ...;Session session = SessionFactory.GetSession("MySQLString");session.Insert(companyList); 修改: Company entity = new Company();entity.Id = 1;entity.Name = "百度";Session session = SessionFactory.GetSession("MySQLString");session.Update(entity); 批量修改:
Session session = SessionFactory.GetSession("MySQLString");List<Company> companyList = ...;Session session = SessionFactory.GetSession("MySQLString");session.Update(companyList); 删除:
Company company = m_companyList[i];//remove a objectSession session = SessionFactory.GetSession("MySQLString");session.Delete(company);//remove by idsession.Delete(company.Id); 批量删除:
//remove by objectList<Company> companyList = ...;Session session = SessionFactory.GetSession("MySQLString");session.Delete(companyList);//remove by idobject[] ids = new object[]{1,2,3,4,5};session.Delete(ids); 数据库与C#对象映射关系配置: using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Easy4net.Entity { [Table(Name = "company")] public class Company { [Id(Name = "id", Strategy = GenerationType.INDENTITY)] public int? Id{ get; set; } [Column(Name = "company_name")] public String CompanyName{ get; set; } [Column(Name = "industry")] public String Industry { get; set; } [Column(Name = "address")] public String Address { get; set; } } } using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text; namespace Easy4net.Entity { [Table(Name = "employee")] public class Employee { [Id(Name = "id", Strategy = GenerationType.INDENTITY)] public int? Id{ get; set; } [Column(Name = "name")] public String Name{ get; set; } [Column(Name = "age")] public int? Age{ get; set; } [Column(Name = "address")] public String Address{ get; set; } [Column(Name = "created")] public DateTime? Created{ get; set; } [Column(Name = "company_id")] public int? CompanyId{ get; set; } [Column(Name = "company_name", IsInsert = false, IsUpdate = false)] public String CompanyName { get; set; } } } 数据库连接配置 web.config中:
<?xml version="1.0"?><configuration> <connectionStrings> <add name="MySQLString" connectionString="Data Source=localhost;Initial Catalog=test_db;Persist Security Info=True;User ID=root;Password=123456;Min Pool Size=1;Max Pool Size=3;" providerName="MySql.Data.MySqlClient" /> <add name="SqlServerString" connectionString="Data Source=(local);Initial Catalog=Study;User ID=sa;Password=sa;" providerName="System.Data.SqlClient" /> <add name="OracleString" connectionString="Data Source=192.168.0.2;Initial Catalog=test;Persist Security Info=True;User ID=user1;Password=pass1" providerName="System.Data.OracleClient" /> <add name="SQLiteString" connectionString="Data Source=|DataDirectory|\testdb.sqlite;Pooling=true;" providerName="System.Data.SQLite" /> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration> |
请发表评论