ORM-SqlSugar使用

Nuget:

PM=> Install-Package SqlSugarCore

 

 基本使用:

1.Mysql数据库:

 

 

 2.创建控制台程序

// See https://aka.ms/new-console-template for more information
using SqlSugar;
 

SqlSugarClient db = new SqlSugarClient(new ConnectionConfig
{
    ConnectionString = "Server=localhost;User ID=ADMIN;Password=ADMIN;port=3306;Database=TB666;CharSet=utf8;pooling=true;SslMode=None;",

    DbType = DbType.MySql,
    IsAutoCloseConnection = true

});
Console.WriteLine("----------通过Mapping进行查询:Mapping------------------------");
//通过Mapping进行查询
var person = db.SqlQueryable<Person>("select ID,  Name,  Age,  Address from person").ToList();
foreach (var item in person)
{
    Console.WriteLine(item);
}

Console.WriteLine("----------直接执行SQL语句:DataTable------------------------");

//直接执行SQL语句:
var dt=db.Ado.GetDataTable("select * from person");
for (int i = 0; i < dt.Rows.Count; i++)
{
    for (int j = 0; j < dt.Columns.Count; j++)
    {
        Console.Write(dt.Columns[j].ColumnName+":"+dt.Rows[i][dt.Columns[j].ColumnName].ToString() +" ");
    }
    Console.WriteLine();
}

public class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string Address { get; set; } 
    public override string ToString()
    {
        return $"ID:{ID} Name:{Name} Age:{Age} Address:{Address}";
    }
}

看看结果:

 

 

 

在Asp.Net Core中使用

 

 

 public class DbContext
    {
        public static SqlSugarClient db = new SqlSugarClient(new ConnectionConfig
        {
            ConnectionString = "Server=localhost;User ID=Admin;Password=Admin;port=3306;Database=TB666;CharSet=utf8;pooling=true;SslMode=None;",

            DbType = DbType.MySql,
            IsAutoCloseConnection = true

        });
        public static void InitDataBase()
        {
            //初始化数据库
            //db.DbMaintenance.CreateDatabase("PERSON");
            //string nspace = "Model.Entity";
            //Type[] ass = Assembly.LoadFrom("bin/Debug/net6.0/Model.dll").GetTypes().Where(p => p.Namespace == nspace).ToArray();
            //db.CodeFirst.SetStringDefaultLength(200).InitTables(ass);
            //模拟测试数据 
            List<Person> people = new List<Person> { 
             new Person(){ID=1,Name="zhangsan",Age=1,Address="北极" },
             new Person(){ID=2,Name="zhangsan2",Age=1,Address="北极" },
             new Person(){ID=3,Name="zhangsan3",Age=1,Address="北极" },
             new Person(){ID=4,Name="zhangsan4",Age=1,Address="北极" },
             new Person(){ID=5,Name="zhangsan5",Age=1,Address="北极" },
             new Person(){ID=6,Name="zhangsan6",Age=1,Address="北极" }
            };
            //写入测试数据
            db.Insertable(people).ExecuteCommand();
        }
    }
    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public override string ToString()
        {
            return $"ID:{ID} Name:{Name} Age:{Age} Address:{Address}";
        }
    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 




posted @ 2022-07-07 21:42  后跳  阅读(174)  评论(0编辑  收藏  举报