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

本文详细介绍了如何利用MySoft.Data进行数据库更新操作,包括强类型数据更新、UpdateCreator数据更新、用户自定义更新方式及事务处理。同时,提供了批量更新、创建外部数据库链接和事务方式的更新方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    前一章讲了如何利用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 DEBUG
            this.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值