SqlSugar/Src/Asp.Net/SqlServerTest/Demos/2_Update.cs

68 lines
2.6 KiB
C#
Raw Normal View History

2017-05-22 02:27:40 +08:00
using OrmTest.Models;
using SqlSugar;
using System;
2017-05-21 22:33:21 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest.Demo
{
2017-05-29 01:30:23 +08:00
public class Update : DemoBase
2017-05-21 22:33:21 +08:00
{
2017-05-29 01:30:23 +08:00
public static void Init()
{
2017-05-22 02:27:40 +08:00
var db = GetInstance();
var updateObj = new Student() { Id = 1, Name = "jack", SchoolId = 0, CreateTime = Convert.ToDateTime("2017-05-21 09:56:12.610") };
var updateObjs = new List<Student>() { updateObj, new Student() { Id = 2, Name = "sun", SchoolId = 0 } }.ToArray();
db.IgnoreColumns.Add("TestId", "Student");
//db.MappingColumns.Add("id","dbid", "Student");
//update reutrn Update Count
2017-05-29 01:30:23 +08:00
var t1 = db.Updateable(updateObj).ExecuteCommand();
2017-05-22 02:27:40 +08:00
//Only update Name
var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ExecuteCommand();
2017-06-04 12:50:30 +08:00
var t3_1 = db.Updateable(updateObj).UpdateColumns(it => it=="Name").ExecuteCommand();
2017-05-22 02:27:40 +08:00
//Ignore Name and TestId
var t4 = db.Updateable(updateObj).IgnoreColumns(it => new { it.Name, it.TestId }).ExecuteCommand();
//Ignore Name and TestId
var t5 = db.Updateable(updateObj).IgnoreColumns(it => it == "Name" || it == "TestId").With(SqlWith.UpdLock).ExecuteCommand();
//Use Lock
var t6 = db.Updateable(updateObj).With(SqlWith.UpdLock).ExecuteCommand();
//update List<T>
var t7 = db.Updateable(updateObjs).ExecuteCommand();
//Re Set Value
var t8 = db.Updateable(updateObj)
.ReSetValue(it => it.Name == (it.Name + 1)).ExecuteCommand();
//Where By Expression
var t9 = db.Updateable(updateObj).Where(it => it.Id == 1).ExecuteCommand();
2017-05-24 21:23:59 +08:00
2017-05-25 01:44:49 +08:00
//Update By Expression Where By Expression
var t10 = db.Updateable<Student>()
2017-05-29 01:30:23 +08:00
.UpdateColumns(it => new Student() { Name = "a", CreateTime = DateTime.Now })
2017-05-24 21:23:59 +08:00
.Where(it => it.Id == 11).ExecuteCommand();
2017-05-25 11:50:08 +08:00
//Rename
2017-05-25 12:39:51 +08:00
db.Updateable<School>().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommand();
2017-05-25 11:50:08 +08:00
//Update Student set Name='jack' Where Id=1
2017-06-09 20:44:59 +08:00
//Column is null no update
db.Updateable(updateObj).Where(true).ExecuteCommand();
2017-09-05 12:52:07 +08:00
var t12= db.Updateable<School>().AS("Student").UpdateColumns(it => new School() { Name = "jack" }).Where(it => it.Id == 1).ExecuteCommandAsync();
t12.Wait();
2017-05-22 02:27:40 +08:00
}
2017-05-21 22:33:21 +08:00
}
}