C/S开发框架教程:使用基础资料(数据字典)的DAL层更新主从表(或多表)数据
C/S开发框架教程:使用基础资料(数据字典)的DAL层更新主从表(或多表)数据
扫一扫加作者微信
CSFramework C/S开发框架作为标准三层开发框架,为用户提供大量的例子参考,快速入门,实战开发。
界面层【测试按钮】的Click事件:
C# Code:
private void button1_Click(object sender, EventArgs e)
{
//获取采购订单PO的数据表结构
var bll = new bllPO();
var ds = bll.GetDataByKey("-", false);
//主表:添加一条记录
var R1 = ds.Tables[tb_PO.__TableName].Rows.Add();
R1[tb_PO.PODate] = DateTime.Today;
R1[tb_PO.Amount] = 15000;
//明细表:添加记录1
var D1 = ds.Tables[tb_POs.__TableName].Rows.Add();
D1[tb_POs.ProductCode] = "CS5";
D1[tb_POs.Quantity] = 100;
D1[tb_POs.Price] = 50;
D1[tb_POs.Amount] = 5000;
//明细表:添加记录2
var D2 = ds.Tables[tb_POs.__TableName].Rows.Add();
D2[tb_POs.ProductCode] = "CS4";
D2[tb_POs.Quantity] = 200;
D2[tb_POs.Price] = 50;
D2[tb_POs.Amount] = 10000;
//模拟使用基础资料(数据字典)的DAL层更新主从表(或多表)数据
var result = bll.UpdateByDatadictDAL(ds);
Msg.ShowInformation($"保存{(result ? "成功" : "失败")}");
//模拟使用基础资料(数据字典)的DAL层更新主从表(或多表)数据
var result1 = bll.UpdateByDatadictDAL_ex(ds);
Msg.ShowInformation($"保存{(result1.Success ? "成功" : "失败")},单据号码:{result1.PrimaryKey}");
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
{
//获取采购订单PO的数据表结构
var bll = new bllPO();
var ds = bll.GetDataByKey("-", false);
//主表:添加一条记录
var R1 = ds.Tables[tb_PO.__TableName].Rows.Add();
R1[tb_PO.PODate] = DateTime.Today;
R1[tb_PO.Amount] = 15000;
//明细表:添加记录1
var D1 = ds.Tables[tb_POs.__TableName].Rows.Add();
D1[tb_POs.ProductCode] = "CS5";
D1[tb_POs.Quantity] = 100;
D1[tb_POs.Price] = 50;
D1[tb_POs.Amount] = 5000;
//明细表:添加记录2
var D2 = ds.Tables[tb_POs.__TableName].Rows.Add();
D2[tb_POs.ProductCode] = "CS4";
D2[tb_POs.Quantity] = 200;
D2[tb_POs.Price] = 50;
D2[tb_POs.Amount] = 10000;
//模拟使用基础资料(数据字典)的DAL层更新主从表(或多表)数据
var result = bll.UpdateByDatadictDAL(ds);
Msg.ShowInformation($"保存{(result ? "成功" : "失败")}");
//模拟使用基础资料(数据字典)的DAL层更新主从表(或多表)数据
var result1 = bll.UpdateByDatadictDAL_ex(ds);
Msg.ShowInformation($"保存{(result1.Success ? "成功" : "失败")},单据号码:{result1.PrimaryKey}");
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
BLL层的方法:
C# Code:
public bool UpdateByDatadictDAL(DataSet ds)
{
//更新公共字段默认值
foreach (DataTable T in ds.Tables) this.UpdateCommonFieldsValue(T);
//保存数据
return new dalPO_DataDictUpdate(Loginer.CurrentUser).Update(ds);
}
public SaveResultEx UpdateByDatadictDAL_ex(DataSet ds)
{
//更新公共字段默认值
foreach (DataTable T in ds.Tables) this.UpdateCommonFieldsValue(T);
//保存数据
return new dalPO_DataDictUpdate(Loginer.CurrentUser).UpdateEx(ds);
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
public bool UpdateByDatadictDAL(DataSet ds)
{
//更新公共字段默认值
foreach (DataTable T in ds.Tables) this.UpdateCommonFieldsValue(T);
//保存数据
return new dalPO_DataDictUpdate(Loginer.CurrentUser).Update(ds);
}
public SaveResultEx UpdateByDatadictDAL_ex(DataSet ds)
{
//更新公共字段默认值
foreach (DataTable T in ds.Tables) this.UpdateCommonFieldsValue(T);
//保存数据
return new dalPO_DataDictUpdate(Loginer.CurrentUser).UpdateEx(ds);
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
DAL层的方法:
C# Code:
/// <summary>
/// 使用基础资料(数据字典)的DAL层更新主从表(或多表)数据
/// </summary>
public class dalPO_DataDictUpdate : dalBaseDataDict
{
public dalPO_DataDictUpdate(Loginer loginer)
: base(loginer)
{
_KeyName = tb_PO.__KeyName; //主键字段
_TableName = tb_PO.__TableName;//表名
_ModelType = typeof(tb_PO);
_UpdateSummaryKeyMode = UpdateKeyMode.OnlyDocumentNo;//自动生成单据号码
}
/// <summary>
/// 根据表名获取该表对应的SQL命令生成器
/// </summary>
/// <param name="tableName">表名</param>
/// <returns></returns>
protected override IGenerateSqlCommand CreateSqlGenerator(DataTable table)
{
Type ORM = null;
if (table.TableName == tb_PO.__TableName) ORM = typeof(tb_PO);
if (table.TableName == tb_POs.__TableName) ORM = typeof(tb_POs);
if (ORM == null) throw new Exception(table.TableName + "表没有ORM模型!");
return new GenerateSqlCmdByTableFields(ORM, table, this.GeneratorFactory);
}
/// <summary>
/// 重写方法,获取单据号码并且设置明细表外键的值
/// </summary>
/// <param name="tran">当前事务</param>
/// <returns></returns>
protected override string GetNumber(DbTransaction tran)
{
string docNo = DocNoTool.GetDocNo(_Database, "PO");//获取单据号码
if (!String.IsNullOrWhiteSpace(docNo)) SetDetailKey(docNo);//设置明细表外键的值
return docNo;
}
/// <summary>
/// 设置明细表的外键
/// </summary>
/// <param name="docNo">主表的主键</param>
private void SetDetailKey(string docNo)
{
foreach (DataRow R in _CurrentDataSet4Update.Tables[tb_POs.__TableName].Rows)
{
R[tb_POs.PONO] = docNo;
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
/// 使用基础资料(数据字典)的DAL层更新主从表(或多表)数据
/// </summary>
public class dalPO_DataDictUpdate : dalBaseDataDict
{
public dalPO_DataDictUpdate(Loginer loginer)
: base(loginer)
{
_KeyName = tb_PO.__KeyName; //主键字段
_TableName = tb_PO.__TableName;//表名
_ModelType = typeof(tb_PO);
_UpdateSummaryKeyMode = UpdateKeyMode.OnlyDocumentNo;//自动生成单据号码
}
/// <summary>
/// 根据表名获取该表对应的SQL命令生成器
/// </summary>
/// <param name="tableName">表名</param>
/// <returns></returns>
protected override IGenerateSqlCommand CreateSqlGenerator(DataTable table)
{
Type ORM = null;
if (table.TableName == tb_PO.__TableName) ORM = typeof(tb_PO);
if (table.TableName == tb_POs.__TableName) ORM = typeof(tb_POs);
if (ORM == null) throw new Exception(table.TableName + "表没有ORM模型!");
return new GenerateSqlCmdByTableFields(ORM, table, this.GeneratorFactory);
}
/// <summary>
/// 重写方法,获取单据号码并且设置明细表外键的值
/// </summary>
/// <param name="tran">当前事务</param>
/// <returns></returns>
protected override string GetNumber(DbTransaction tran)
{
string docNo = DocNoTool.GetDocNo(_Database, "PO");//获取单据号码
if (!String.IsNullOrWhiteSpace(docNo)) SetDetailKey(docNo);//设置明细表外键的值
return docNo;
}
/// <summary>
/// 设置明细表的外键
/// </summary>
/// <param name="docNo">主表的主键</param>
private void SetDetailKey(string docNo)
{
foreach (DataRow R in _CurrentDataSet4Update.Tables[tb_POs.__TableName].Rows)
{
R[tb_POs.PONO] = docNo;
}
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
提交的数据:
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网