• ORMSqlSugar使用


    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}";
            }
        }



  • 相关阅读:
    【linux】驱动-5-驱动框架分层分离&实战
    【linux】驱动-4-LED芯片手册分析
    【MCU】国民N32固件库移植
    【MCU】移植AT32库&FreeRTOS教程
    P3768 简单的数学题
    P4301 [CQOI2013] 新Nim游戏
    P4767 [IOI2000]邮局
    P3211 [HNOI2011]XOR和路径
    FWT 笔记
    P3175 [HAOI2015]按位或(max-min 容斥)
  • 原文地址:https://www.cnblogs.com/Zingu/p/16456252.html
Copyright © 2020-2023  润新知