项目作者: qinyuanpei

项目描述 :
基于JSON的轻量级数据库
高级语言: C#
项目地址: git://github.com/qinyuanpei/JSON4DB.git
创建时间: 2015-12-02T09:44:36Z
项目社区:https://github.com/qinyuanpei/JSON4DB

开源协议:

下载


JSON4DB

JSON4DB是一个基于JSON的轻量级数据库,它使用JSON来作为数据的组织形式,是一个面向对象的轻量级数据库。
该数据库使得开发者在编写数据库应用的时候,无需再去关注SQL语句,因为在这里一切都是对象,从数据库到数据,
程序员完全可以用它最为熟悉的面向对象编程,我编写这样一个项目的初衷就是让数据库变得更为简单,在我们希望
存储简单的数据结构的时候,这个项目能够让我们从SQLServer、MySQL等这些复杂的数据库中解脱出来,可能这个项目
无法承载“大数据”的需求,然而对我来说,简单到好用这就足够了!

代码示例

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using JSON4DB;
  7. namespace JSON4DBSample
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. //创建数据库
  14. JSON4DB<Student> db = JSON4DB<Student>.Create("test.json");
  15. //插入一条数据
  16. db.Insert("001", new Student(001, "张三", 12));
  17. //提交更改
  18. db.Commit();
  19. //加载数据库
  20. db = JSON4DB<Student>.Load("test.json");
  21. //删除ID为001的记录
  22. db.Delete("001");
  23. //插入三条数据
  24. db.Insert("001", new Student(002, "张三", 12));
  25. db.Insert("002", new Student(003, "李四", 20));
  26. db.Insert("003", new Student(004, "王五", 25));
  27. //更新ID为003的记录
  28. db.Update("003", new Student(001, "Wang5", 15));
  29. //提交更改
  30. db.Commit();
  31. //读取ID为002的记录
  32. Student stu = db.Read("002");
  33. //读取所有记录返回一个列表
  34. List<Student> studentList = db.ReadList();
  35. //读取所有记录返回一个数组
  36. Student[] students = db.Read();
  37. Console.WriteLine(students[0].ToString());
  38. Console.ReadKey();
  39. }
  40. }
  41. /// <summary>
  42. /// 定义一个示例类Student
  43. /// </summary>
  44. public class Student
  45. {
  46. public Student()
  47. {
  48. }
  49. public Student(int mID, string mName, int mAge)
  50. {
  51. this.mID = mID;
  52. this.mName = mName;
  53. this.mAge = mAge;
  54. }
  55. private int mAge;
  56. public int Age
  57. {
  58. get { return mAge; }
  59. set { mAge = value; }
  60. }
  61. private int mID;
  62. public int ID
  63. {
  64. get { return mID; }
  65. set { mID = value; }
  66. }
  67. private string mName;
  68. public string Name
  69. {
  70. get { return mName; }
  71. set { mName = value; }
  72. }
  73. public override string ToString()
  74. {
  75. return string.Format("ID={0},Name={1},Age={2}", this.ID, this.Name, this.Age);
  76. }
  77. }
  78. }