SqlSugar realizes data access

The "sqlsugar" component implements data access

SqlSugar is a small and full-featured ORM that does not need to rely on third-party extensions like Dapper. The syntax is easy to use and simple, with beautiful lambda syntax, and also supports Dapper SQL and ADO.NET (stored procedures, etc.). All functions, its performance reaches the native level, far surpassing Dapper and EF CORE, and supports .NET CORE, multiple databases

    1. 连接数据库   
    1)添加一个数据表的类定义其属性
       class Customer
    {
        public string customerNo
        {
            get;
            set;
        }
        public string customerName
       {
           get;
           set;
       }
        public string zip
        {
            get;
            set;
        }
        public string address
        {
            get;
            set;
        }
        public string telephone
        {
            get;
            set;
        }
    }

2) Add another class to connect to the database

  public class SugarDao
    {
       public static string ConnectionString
       {
           get
           {
               string reval = "Data Source=.;Integrated Security=SSPI;Initial Catalog=OrderDB";
               return reval;
           }
       }
       public static SqlSugarClient GetInstance()
       {
           var db = new SqlSugarClient(ConnectionString);
           return db;
       }
    }

3) Main function part

var db = SugarDao.GetInstance();
    2. 查询

1) Query a single

 var single = db.Queryable<Customer>().Single(c => c.customerNo == "C20050002");
 Console.WriteLine("{0}  {1}  {2}  {3}  {4}", single.customerNo, single.customerName, single.address, single.telephone, single.zip);

2) Query a single item according to the primary key

var singleByPk = db.Queryable<Customer().InSingle("C20050001");
 Console.WriteLine("{0}  {1}  {2}  {3}  {4}", singleByPk.customerNo, singleByPk.customerName, singleByPk.address, singleByPk.telephone, singleByPk.zip);

3) Query all customerNo

var singleFieldList = db.Queryable<Customer>().Select<string>(it => it.customerNo).ToList();
   foreach (var item in singleFieldList)
        {
           Console.WriteLine(item);
        }

4) Query multiple items

 List<Customer> cusList = db.Queryable<Customer>().ToList();
            foreach (Customer i in cusList)
            {
                Console.WriteLine("{0}  {1}  {2}  {3}  {4}", i.customerNo, i.customerName, i.address, i.telephone, i.zip);
            }

5) Fuzzy query

 Console.WriteLine("查询客户名称中包含'市'的");
            var c1 = db.Queryable<Customer>().Where(c => c.customerName.Contains("市")).ToList();
            foreach (Customer i in c1)
            {
                Console.WriteLine("{0}  {1}  {2}  {3}  {4}", i.customerNo, i.customerName, i.address, i.telephone, i.zip);
            }
            Console.WriteLine("查询客户名称是'南'字开头的");
            var c2 = db.Queryable<Customer>().Where(c => c.customerName.StartsWith("南")).ToList();
            foreach (Customer i in c2)
            {
                Console.WriteLine("{0}  {1}  {2}  {3}  {4}", i.customerNo, i.customerName, i.address, i.telephone, i.zip);
            }
             Console.WriteLine("查询客户名称是'城'字结尾的");
            var c3 = db.Queryable<Customer>().Where(c => c.customerName.EndsWith("城")).ToList();
            foreach (Customer i in c3)
            {
                Console.WriteLine("{0}  {1}  {2}  {3}  {4}", i.customerNo, i.customerName, i.address, i.telephone, i.zip);
            }
   3. 插入

1) The column of attribute zip will not insert values

 db.DisableInsertColumns = new string[] { "zip" };

2) Add a column that prohibits inserting

 db.AddDisableInsertColumns("telephone", "address");//如果插入的列本身为not null,那么数据将无法插入到数据库
Customer cus = new Customer()
            {
                customerNo = "C20080002",
                customerName = "南昌市电脑研制中心",
                telephone = "215455452",
                address = "南昌市",
                zip = "000222"
            };


  Customer cus1 = new Customer()
            {
                customerNo = "C20080003",
                customerName = "南昌市电脑研制中心",
                telephone = "215455333",
                address = "南昌市",
                zip = "222555"
            };

3) Insert a single

 var id = db.Insert(cus); 

4) Insert multiple

   List<Customer> clist = new List<Customer>();
            clist.Add(cus);
            clist.Add(cus1);

5) Bulk insert

var id1 = db.InsertRange(clist);

6) Batch insertion is suitable for mass data insertion

 var id2 = db.SqlBulkCopy(clist);

7) Clear the forbidden insert column

db.DisableInsertColumns = null;
 4. 更新

1) Only update the attribute zip with the condition customerNo == "C20050001"

  db.Update<Customer>(new { zip = "111000" }, it => it.customerNo == "C20050001");

2) Update two columns of attribute zip and attribute telephone

 db.Update<Customer, string>(new { zip = "112311", telephone = "012-45781411" }, "C20050001", "C20050002");//根据主键,可以有多个参数,结果修改后的多个人的zip,telephone都相同

3) use dictionary update

 var dic = new Dictionary<string, string>();
            dic.Add("zip", "112333");
            dic.Add("telephone", "012-45781433");
            db.Update<Customer, string>(dic, "C20050001");

4) The whole entity is updated
Method 1:

  db.Update(new Customer { customerNo = "C20050001", customerName = "蓝翔16", zip = "112311", address = "北京市", telephone = "012-45781411" });

Method Two

  db.Update<Customer>(new Customer { customerName = "蓝翔14", zip = "112333", address = "上海市", telephone = "012-45781422" }, it => it.customerNo == "C20050001");

Notice:

     db.Update<Customer>(new Customer() { customerNo = "C20050001", customerName = "蓝翔16" });//这里只更新某几列是可以实现的,未添加值的列将为null,所以若属性设置为了not null会更新失败

5) Set the column not to update

      db.DisableUpdateColumns = new string[] { "telephone" };//设置telephone不更新
     Customer cus = new Customer()
            {
                customerNo = "C20050001",
                customerName = "南昌市电脑研制中心",
                address = "南昌市",
                zip = "000222"
            };
            Customer cus1 = new Customer()
            {
                customerNo = "C20050002",
                customerName = "南昌市电脑研制中心",
                address = "上海市",
                zip = "222000"
            };

6) Update a single

      var id = db.Update(cus); 

7) Update multiple items

      List<Customer> clist = new List<Customer>();
            clist.Add(cus);
            clist.Add(cus1);

8) Batch update
Method 1:

     var id1 = db.UpdateRange(clist);

Method Two:

    var id2 = db.SqlBulkReplace(clist);// 适合海量数据更新

9) Clear the forbidden update column

    db.DisableUpdateColumns = null;
5. 删除

1) According to the primary key

    db.Delete<Customer, string>("C20080002");

2) Primary key batch deletion

       db.Delete<Customer, string>(new string[] {"C20080002", "C20080003" });

3) Batch deletion of non-primary keys

    db.Delete<Customer, string>(it => it.address, new string[] { "000222", "222000" });

4) Delete according to the string

     db.Delete<Customer>(it => it.customerNo == "C20080002");

5) According to the expression

     db.Delete<Customer>("customerNo=@customerNo", new { customerNo = "C20080002" });

6) According to the entity

        db.Delete(new Customer() { customerNo = "C20080003" });

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325894386&siteId=291194637