This commit is contained in:
sunkaixuan 2017-05-22 02:27:40 +08:00
parent c1266e7263
commit 717492b226
3 changed files with 52 additions and 3 deletions

View File

@ -1,4 +1,6 @@
using System; using OrmTest.Models;
using SqlSugar;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -6,7 +8,53 @@ using System.Threading.Tasks;
namespace OrmTest.Demo namespace OrmTest.Demo
{ {
class Update public class Update
{ {
public static void Init() {
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
var t1= db.Updateable(updateObj).ExecuteCommand();
//Only update Name
var t3 = db.Updateable(updateObj).UpdateColumns(it => new { it.Name }).ExecuteCommand();
//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();
}
public static SqlSugarClient GetInstance()
{
SqlSugarClient db = new SqlSugarClient(new SystemTablesConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" + db.RewritableMethods.SerializeObject(pars));
Console.WriteLine();
};
return db;
}
} }
} }

View File

@ -37,6 +37,7 @@ namespace OrmTest
OrmTest.Demo.Query.Init(); OrmTest.Demo.Query.Init();
OrmTest.Demo.Insert.Init(); OrmTest.Demo.Insert.Init();
OrmTest.Demo.Delete.Init(); OrmTest.Demo.Delete.Init();
OrmTest.Demo.Update.Init();
} }
} }
} }

View File

@ -25,7 +25,7 @@ namespace SqlSugar
public int ExecuteCommand() public int ExecuteCommand()
{ {
PreToSql(); PreToSql();
return this.Ado.ExecuteCommand(UpdateBuilder.ToSqlString(), UpdateBuilder.Parameters.ToArray()); return this.Ado.ExecuteCommand(UpdateBuilder.ToSqlString(), UpdateBuilder.Parameters==null?null:UpdateBuilder.Parameters.ToArray());
} }
public IUpdateable<T> IgnoreColumns(Func<string, bool> ignoreColumMethod) public IUpdateable<T> IgnoreColumns(Func<string, bool> ignoreColumMethod)