天天看点

cuyan.orm 粗盐工具系列“ORM” (介绍与使用:版本 1.03)

最近总是要写一些插件,然后需求的运行环境版本很低,大部分都是 XP 系统。在连接数据库做一些操作的时候,就会用到 sqlhelper,或者ORM,这个时候才发现大部分的ORM都是目前不支持低版本。.NET Framework 2.0 ,所以我就自己写了一个类似的,希望帮自己提升一些开发插件的速度。)

C# cuyan.orm 粗盐工具系列“ORM”,数据库的 增删查改。

  • ​​环境​​
  • ​​调用的代码和说明​​

代码开源,在下面这个链接:

​​https://gitee.com/daolizhe/cuyan.orm​​

环境

.net framework 2.0 +

nuget : cuyan.orm

cuyan.orm 粗盐工具系列“ORM” (介绍与使用:版本 1.03)

安装后引用 using cuyan.orm;

调用的代码和说明

var _db = new CuyanClient()
{
    ConnectionString = "Server=120.79.19.125;Initial Catalog=cuyan;User ID=cuyan;Password="
};

//新增
Console.WriteLine(_db.Add(new Student() { Gender = 1, Name = "张三", IsGraduate = false }));

//查询
var students = _db.Query<Student>("SELECT * FROM[cuyan].[dbo].[Student]", null);

//修改
students[0].Gender = 0;
students[0].IsGraduate = false;
_db.Update(students[0]);

//删除
_db.Delete(students[0]);      
using cuyan.orm;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
namespace ORM
{

    public class TestName
    {
        public string Name { get; set; }
    }

    public class Student
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public int Gender { get; set; }
        public bool IsGraduate { get; set; }
    }


    public class StudentModel
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public int Gender { get; set; }
        public int IsGraduate { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var _db = new CuyanClient()
            {
                ConnectionString = "Server=120.79.19.125;Initial Catalog=cuyan;User ID=cuyan;Password=123456"
            };

            //新增
            var colorTemp = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"======= 新增 =======");
            Console.ForegroundColor = colorTemp;
            if (true)
            {
                var result = _db.Add(new Student() { Gender = 1, Name = "张三", IsGraduate = false });
                if (result > 0)
                    Console.WriteLine($"受影响数据:{result} 条。(添加成功)");
                else
                    Console.WriteLine($"受影响数据:{result} 条。(添加失败)");
            }
            Console.WriteLine("\n");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"======= 查询 =======");
            Console.ForegroundColor = colorTemp;
            //查询
            if (true)
            {
                var students = _db.Query<Student>("SELECT * FROM[cuyan].[dbo].[Student]", null);
                if (students != null && students.Count > 0)
                {
                    Console.WriteLine($"查询到数据 {students.Count} 条");
                    Console.WriteLine($"展示第一条:名字:{students[0].Name},是否毕业:{students[0].IsGraduate}");
                }
            }
            Console.WriteLine("\n");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"======= 修改更新 =======");
            Console.ForegroundColor = colorTemp;
            //修改更新
            if (true)
            {
                var students = _db.Query<Student>("SELECT * FROM[cuyan].[dbo].[Student]", null);
                if (students != null && students.Count > 0)
                {
                    //修改数据
                    students[0].IsGraduate = true;
                    //更新数据
                    var result = _db.Update(students[0]);
                    if (result > 0)
                        Console.WriteLine($"受影响数据:{result} 条。(修改更新成功)");
                    else
                        Console.WriteLine($"受影响数据:{result} 条。(修改更新失败)");
                }
            }
            Console.WriteLine("\n");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"======= 删除 =======");
            Console.ForegroundColor = colorTemp;
            //删除
            if (true)
            {
                var students = _db.Query<Student>("SELECT * FROM[cuyan].[dbo].[Student]", null);
                if (students != null && students.Count > 0)
                {
                    //删除(根据主键删除)
                    var result = _db.Delete(students[0]);
                    if (result > 0)
                        Console.WriteLine($"受影响数据:{result} 条。(删除成功)");
                    else
                        Console.WriteLine($"受影响数据:{result} 条。(删除失败)");
                }
            }

            Console.WriteLine("\n");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"======= 映射对象 Mapping =======");
            Console.ForegroundColor = colorTemp;
            //映射对象 Mapping
            if (true)
            {
                var students = _db.Query<Student>("SELECT * FROM[cuyan].[dbo].[Student]", null);
                var studentModels = Mapper.MapList<Student, StudentModel>(students);

                if (studentModels != null && studentModels.Count > 0)
                {
                    Console.WriteLine($"查询到数据 {students.Count} 条,转换数据 {studentModels.Count} 条");
                    Console.WriteLine($"展示第一条:名字:{studentModels[0].Name},是否毕业:{studentModels[0].IsGraduate}");
                }
            }

            Console.ReadKey();
        }
    }
}