日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MySoft.Data从入门到精通系列(五)【数据更新】

發布時間:2025/4/14 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySoft.Data从入门到精通系列(五)【数据更新】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

??? 前一章講了如何利用MySoft.Data進行數據的插入,利用DbSession可以實現各種數據增、刪、改、查等各種復雜的處理,本章著重講解一下數據的更新:

??? 數據更新在日常開發中占據中非常重要的地位,盡次于查詢。下面就講解一下如何利用DbSession來進行數據的更新。

??? 繼續引用前面的DbSession配置,如下:

代碼/// <summary>/// 數據庫訪問類/// </summary>public static class DataAccess{/// <summary>/// 通過配置節來實例化DbSession/// </summary>public static readonly DbSession DefaultSession = new DbSession("DataExample");/// <summary>/// 通過自定義類來實例化DbSession/// </summary>public static readonly DataExample ExampleSession = new DataExample();}/// <summary>/// DataExample會話類/// </summary>public class DataExample : DbSession{public DataExample(): base("DataExample"){ #if DEBUGthis.RegisterSqlLogger(log =>{System.IO.File.WriteAllText("c:\\log.txt", log);}); #endif}}

?

下面還是利用DataAccess.ExampleSession來進行操作:

?

一、強類型的數據更新

?

下面的操作以Products實體為例進行操作:

1、單個實體數據更新

代碼 //實例化一個Products對象Products product = new Products(){ProductID = 1,ProductName = "測試產品1"};//更新單個對象product.Attach();DataAccess.ExampleSession.Save(product);

2、批量實體數據更新

代碼 //實例化一組Products對象List<Products> list = new List<Products>();for (int index = 0; index < 10; index++){list.Add(new Products(){ProductID = index,ProductName = "測試產品" + index});}//批量更新數據DbBatch batch = DataAccess.ExampleSession.BeginBatch(10);list.ForEach(item =>{item.Attach();batch.Save(item);});batch.Process();

3、帶事務單個實體更新(MySoft.Data內置實現DbTrans)

代碼 //實例化一個Products對象Products product = new Products(){ProductID = 1,ProductName = "測試產品1"};//使用事務進行數據插入using (DbTrans trans = DataAccess.ExampleSession.BeginTrans()){try{product.Attach();trans.Save(product);trans.Commit();}catch{trans.Rollback();}}

4、帶事務批量實體更新(MySoft.Data內置實現DbTrans)

代碼//實例化一組Products對象List<Products> list = new List<Products>();for (int index = 0; index < 10; index++){list.Add(new Products(){ProductID = index,ProductName = "測試產品" + index});}//使用事務進行批量數據插入using (DbTrans trans = DataAccess.ExampleSession.BeginTrans()){try{DbBatch batch = trans.BeginBatch(10);list.ForEach(item =>{item.Attach();batch.Save(item);});batch.Process();trans.Commit();}catch{trans.Rollback();}}

5、創建外部數據庫鏈接方式更新

代碼//實例化一個Products對象Products product = new Products(){ProductID = 1,ProductName = "測試產品1"};using (System.Data.Common.DbConnection conn = DataAccess.ExampleSession.CreateConnection()){//更新單個對象product.Attach();DataAccess.ExampleSession.SetConnection(conn).Save(product);}

注:批量插入可以采用同樣的方法處理!

6、創建外部數據庫事務方式更新

代碼//實例化一個Products對象Products product = new Products(){ProductID = 1,ProductName = "測試產品1"};using (System.Data.Common.DbTransaction trans = DataAccess.ExampleSession.BeginTransaction()){try{//更新單個對象product.Attach();DataAccess.ExampleSession.SetTransaction(trans).Save(product);trans.Commit();}catch{trans.Rollback();}}

注:批量更新可以采用同樣的方法處理!

?

當實體存在時更新,否則插入的處理方式:

代碼 Products product = new Products(){ProductID = 1,ProductName = "測試產品"};DataAccess.ExampleSession.InsertOrUpdate(product);

?

以上操作相應的都可以使用事務來處理

?

二、UpdateCreator數據更新

通過更新創建器同樣也可以達到上面的效果,也可以進行泛型方式進行數據插入,一般情況下創建器用于沒有建立對象實體時直接對表和字段的操作。

1、通過實體更新實體

代碼 UpdateCreator uc = UpdateCreator.NewCreator().From<Products>().SetEntity<Products>(product, true);DataAccess.ExampleSession.Excute(uc);

2、通過字符串表與字段更新數據

代碼 UpdateCreator uc = UpdateCreator.NewCreator().From("Products").AddUpdate("ProductName", "測試產品").AddWhere("ProductID", 1);DataAccess.ExampleSession.Excute(uc);

三、用戶自定義更新方式

DataAccess.ExampleSession.Update<Products>(Products._.ProductName, "測試產品", Products._.ProductID == 1);

?

條件可以多個組件產生,如下:

WhereClip where = Where.All;

if(條件一) where = where && Products._.ProductID == 1;

if(條件二) where = where && Products._.ProductID == 2;

//ProductID為1和2(相當于In操作)

if(條件三) where = where || Products._.ProductID.In(1,2);

//ProductID不為1和2(相當于Not In操作)

if(條件四) where = where || !Products._.ProductID.In(1,2);

……

DataAccess.ExampleSession.Update<Products>(Products._.ProductName, "測試產品", where);


以上通過創建器的方式同樣可以用事務來操作 trans.Excute(uc);

這里只是簡單的介紹了一下,還有更多的功能需要用戶使用時才能體會到。

數據的更新操作就講解到這里,下一章將講解數據的刪除(Delete)操作

?

有什么問題可以到此處:MySoft組件問題反饋與疑難解答

轉載于:https://www.cnblogs.com/maoyong/archive/2010/04/13/1710913.html

總結

以上是生活随笔為你收集整理的MySoft.Data从入门到精通系列(五)【数据更新】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。