FreeSql (十)更新資料

2022-01-13 05:20:24 字數 3737 閱讀 1557

freesql提供豐富的資料庫更新功能,支援單條或批量更新,在特定的資料庫執行還可以返回更新後的記錄。

static ifreesql fsql = new freesql.freesqlbuilder()

.useconnectionstring(freesql.datatype.mysql, connectionstring)

.useautosyncstructure(true) //自動同步實體結構到資料庫

.build(); //請務必定義成 singleton 單例模式

class topic

public int clicks

public string title

public datetime createtime

}

只更新變化的屬性,依賴 freesql.repository

var repo = fsql.getrepository();

var item = repo.where(a => a.id == 1).first(); //此時快照 item

item.title = "newtitle";

repo.update(item); //對比快照時的變化

//update `topic` set `title` = @p_0

//where (`id` = 1)

是不是覺得先查詢再更新,囉嗦?

var repo = fsql.getrepository();

var item = new topic ;

repo.attach(item); //此時快照 item

item.title = "newtitle";

repo.update(item); //對比快照時的變化

//update `topic` set `title` = @p_0

//where (`id` = 1)

方法2:(原始)

忽略更新 null 值的屬性

fsql.update()

.setsourceignore(item, col => col == null)

.executeaffrows();

var item = new topic ;

fsql.update()

.setsource(item)

.executeaffrows();

//update `topic` set `clicks` = @p_0, `title` = @p_1, `createtime` = @p_2

//where (`id` = 1)

fsql.update()

.setsource(item)

.updatecolumns(a => new )

.executeaffrows();

//update `topic` set `title` = @p_0, `createtime` = @p_1

//where (`id` = 1)

fsql.update()

.setsource(item)

.ignorecolumns(a => new )

.executeaffrows();

//update `topic` set `title` = @p_0

//where (`id` = 1)

var items = new list();

for (var a = 0; a < 10; a++) items.add(new topic ", clicks = a * 100 });

fsql.update()

.setsource(items)

.executeaffrows();

//update `topic` set `clicks` = case `id` when 1 then @p_0 when 2 then @p_1 when 3 then @p_2 when 4 then @p_3 when 5 then @p_4 when 6 then @p_5 when 7 then @p_6 when 8 then @p_7 when 9 then @p_8 when 10 then @p_9 end,

//`title` = case `id` when 1 then @p_10 when 2 then @p_11 when 3 then @p_12 when 4 then @p_13 when 5 then @p_14 when 6 then @p_15 when 7 then @p_16 when 8 then @p_17 when 9 then @p_18 when 10 then @p_19 end,

//`createtime` = case `id` when 1 then @p_20 when 2 then @p_21 when 3 then @p_22 when 4 then @p_23 when 5 then @p_24 when 6 then @p_25 when 7 then @p_26 when 8 then @p_27 when 9 then @p_28 when 10 then @p_29 end

//where (`id` in (1,2,3,4,5,6,7,8,9,10))

fsql.update()

.setsource(items)

.ignorecolumns(a => new )

.executeaffrows();

//update `topic` set `title` = case `id` when 1 then @p_0 when 2 then @p_1 when 3 then @p_2 when 4 then @p_3 when 5 then @p_4 when 6 then @p_5 when 7 then @p_6 when 8 then @p_7 when 9 then @p_8 when 10 then @p_9 end

//where (`id` in (1,2,3,4,5,6,7,8,9,10))

fsql.update()

.setsource(items)

.set(a => a.createtime, datetime.now)

.executeaffrows();

//update `topic` set `createtime` = @p_0

//where (`id` in (1,2,3,4,5,6,7,8,9,10))

指定 set 列更新後,setsource 將失效

方法

返回值引數

描述setsource

t1 | ienumerable

更新資料,設定更新的實體

commandtimeout

int命令超時設定(秒)

withtransaction

dbtransaction

設定事務物件

withconnection

dbconnection

設定連線物件

tosql

string

返回即將執行的sql語句

executeaffrows

long

執行sql語句,返回影響的行數

executeupdated

list

執行sql語句,返回更新後的記錄

FreeSql (十三)更新資料時忽略列

static ifreesql fsql new freesql.freesqlbuilder useconnectionstring freesql.datatype.mysql,connectionstring useautosyncstructure true 自動同步實體結構到資料庫 bui...

FreeSql (五)插入資料

var connectionstring data source 127.0.0.1 port 3306 user id root password root initial catalog cccddd charset utf8 sslmode none max pool size 10 stat...

FreeSql 插入資料返回自增值

freesql是乙個功能強大的 net orm 功能庫,支援 netframework 4.0 netcore 2.1 xamarin 等支援 netstandard 所有執行平台。以 mit 開源協議託管於 github freesql 插入資料的方式有多種,這篇文章教你用最優的方案做資料插入功能...