前一章讲了如何利用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组件问题反馈与疑难解答