Skip to content
果糖网 edited this page Jul 2, 2024 · 3 revisions

1、Dictionary update (multi-library support)

// Dictionary
var dt = new Dictionary<string, object>();
dt.Add("id", 1);
dt.Add("name", "jack");
dt.Add("createTime", DateTime.Now);
var t66 = db.Updateable(dt).AS("student").WhereColumns("id").ExecuteCommand();

2、Dictionary collection (multi-library support)

// Dictionary collection
var dtList = new List<Dictionary<string, object>>();
dtList.Add(dt);
dtList.Add(dt2);
var t666 = db.Updateable(dtList).AS("student").WhereColumns("id").ExecuteCommand();

3、Anonymous object update

This method does not support batch, if batch dictionary mode

db.UpdateableByDynamic
(new { id = 1, name = "a" })
.AS("order")
.WhereColumns("id").ExecuteCommand();

//sql
UPDATE [order]  SET
[name]=@name  WHERE [id]=@id
@id:1,@name:a

4、Dynamic object update

ExpandoObject ex = new ExpandoObject();
var dic=  (IDictionary<string, object>)(ex);
dic.Add("name", "1");
dic.Add("id", SnowFlakeSingle.Instance.NextId());
db.Upateable(new Dictionary<string, object>(ex)).AS("StudentWithSnowflake08").ExecuteCommand();

5、SQL mode update

The where condition is too flexible and there may be incompatibilities between different databases

db.Updateable<object>()
.AS("Order")
.SetColumns("name", 1)
.Where("id=1").ExecuteCommand();

6、BulkCopy update (requires a newer version)

The condition is updated based on ID

db.Fastest<DataTable>().AS("Order").BulkUpdate(datatable, new string[] { "id" });

7、Dynamic class update (the most perfect support for multi-library)

The above may be simpler, but this feature is mainly used for products that are more compatible with multiple libraries and can support features such as AOP

var type = db.DynamicBuilder().CreateClass("table1", new SugarTable()
{
})
.CreateProperty("Id",typeof(int),new SugarColumn() {IsPrimaryKey=true,IsIdentity=true })
.CreateProperty("Name",typeof(string), new SugarColumn() { })
.withcache ()// Cache the KEY, which is based on the table name and field name.
.BuilderType();

db.CodeFirst.InitTables(type);
var dic=new Dictionary<string, object>(){{"Id", 1 }, { "Name", "jack"}};
var value=db.DynamicBuilder().CreateObjectByType(type,dic);

db.InsertableByObject(value).ExecuteCommand();
db.UpdateableByObject(value).ExecuteCommand();
db.DeleteableByObject(value).ExecuteCommand();
db.StorageableByObject(value).ExecuteCommand(); // Insert or update
db.QueryableByObject(typeof(OrderSpliteTest)).ToList();