奋斗的博客

开源、创新!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  34 随笔 :: 0 文章 :: 164 评论 :: 95539 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

   之前写过一个系列文章自己动手写ORM框架,经过在多个项目的中的使用,对这套代码进行了许多改进,下面是使用方法:

 新增学员信息代码预览:

复制代码
    DBHelper db = DBHelper.getInstance();
    Student stu = new Student();
  stu.Name = "张三";
  stu.Gender = "";
  stu.Age = 28;
  stu.Address = "上海市徐汇区";
  
int count = db.Save<Student>(stu);   if (count > 0)   {   MessageBox.Show("新增成功!");   }
复制代码

 

  修改学员信息:

  stu.UserID = 1;
  stu.Name = "李四";
  stu.Age = 22;
  db.Update<Student>(stu);

 

  删除学员信息:

  Student student = m_stuList[i];

  //对象删除
  db.Remove<Student>(student);

  //ID删除
  db.Remove<Student>(student.UserID);

 

  查询:

复制代码
   //查询所有学员信息
  List<Student> list = DB.FindAll<Student>();

  //根据ID查询
  Student student = DB.FindById<Student>(5);

  //自定义SQL查询
  List<Student> list1 = DB.FindBySql<Student>("SELECT * FROM U_Student WHERE U_Age < 28");

  //按某个列查询
  List<Student> list2 = DB.FindByProperty<Student>("U_Name", "张三");

  //按精确条件查询,这里是SELECT xxx FROM U_Student WHERE U_Name LIKE '%张%' OR U_Age < 28
  DbCondition cond1 = new DbCondition().Where().Like("U_Name", "").OrLessThan("U_Age", 28);
  List<Student> list3 = DB.Find<Student>(cond1);

  //关联查询,这个不用多说了,会SQL的都知道,查询条件是 WHERE U_Name LIKE '张%'
  DbCondition cond2 = new DbCondition("SELECT s.*,c.teacher,c.className FROM U_Student s INNER JOIN U_Class c ON s.classID = c.ID").Where().RightLike("U_Name", "");
  List<Student> list4 = DB.Find<Student>(cond2);

  //这里是查询 SELECT count(0) FROM U_Student WHERE U_Name = '张三' AND U_Age = 28
  DbCondition cond3 = new DbCondition().Where("U_Name", "张三").And("U_Age", 28);
  int count = DB.FindCount<Student>(cond3);
复制代码

 

实体类配置:

复制代码
namespace Entiry
{
    [Serializable]
    [Table(Name = "U_Student")]
    public class Student
    {
        //主键 INDENTITY自动增长标识
        [Id(Name = "UserID", Strategy = GenerationType.INDENTITY)]
        public int UserID { get; set; }

        //对应数据库中的名字为U_Name
        [Column(Name = "U_Name")]
        public string Name { get; set; }

        [Column(Name = "U_Age")] // int? 允许int为NULL时不会报错
        public int? Age { get; set; }

        [Column(Name = "U_Gender")]
        public string Gender { get; set; }

        [Column(Name = "U_Address")]
        public string Address { get; set; }

        [Column(Name = "U_CreateTime")]
        public DateTime? CreateTime { get; set; }

        [Column(Name = "ClassID")]
        public int? ClassID { get; set; }

        //下面2列 ClassName和Teacher字段是属于班级表中的班级名称和班主任
        //但是因为是外键表,关联的班级编号:ClassID,所以做关联查询可以加这2个属性
        //但是修改和插入则不需要这2列,只做查询,所以加上IsInsert=false,IsUpdate=false
        [Column(Name = "ClassName",IsInsert=false,IsUpdate=false)]
        public string ClassName { get; set; }

        [Column(Name = "Teacher", IsInsert = false, IsUpdate = false)]
        public string Teacher { get; set; }
    }
}
复制代码

 

  最后是配置文件:

  <configuration>
    <appSettings>
      <add key="DbType" value="sqlserver"/>
      <add key="connectionString" value="Data Source=.;Initial Catalog=OrmDB;User ID=test;Password=test;Trusted_Connection=no;Min Pool Size=10;Max Pool Size=100;"/>
  </appSettings>

  只需加连接字符串和数据库类型,因为支持这里可以支持多种数据库。

 

 源码托管在github上,需要的可以去下载。

点击下载:https://github.com/wangwei123/easy4net

 

 

posted on   奋斗  阅读(3173)  评论(5编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示