使用[后台数据更新模型]保存主从表数据(C#源码)
内容介绍
本文演示使用后台数据更新模型保存主从表数据. 另一个学习重点结合UI/BLL/DAL层设计一个简单易用的业务窗体。
图一:展示主从表结构窗体设计
图二:表格内使用LookupEdit控件
主窗体代码:
public partial class frmMasterDetailUpdate : XtraForm
{
private UpdateType _UpdateType = UpdateType.None;
/// <summary>
/// 当前正在处理的业务数据
/// </summary>
private DataSet _CurrentBusiness = null;
/// <summary>
/// 业务逻辑管理类
/// </summary>
private DataManager _DataManager = null;
public frmMasterDetailUpdate()
{
InitializeComponent();
}
private void frmMasterDetailUpdate_Load(object sender, EventArgs e)
{
_UpdateType = UpdateType.Add; //初始化更新状态
_DataManager = new DataManager(DataProvider.Instance.CreateConnection());//初始化业务逻辑
_CurrentBusiness = _DataManager.BuildDemoData();
gcDetail.DataSource = _CurrentBusiness.Tables[1];//绑定明细表格的数据
//绑定参考字段
DataTable lookup = _DataManager.GetProductData();
repositoryItemLookUpEdit1.DisplayMember = "ProductCode";
repositoryItemLookUpEdit1.ValueMember = "ProductCode";
repositoryItemLookUpEdit1.DataSource = lookup;
repositoryItemLookUpEdit2.DisplayMember = "ProductName";
repositoryItemLookUpEdit2.ValueMember = "ProductName";
repositoryItemLookUpEdit2.DataSource = lookup;
this.defaultLookAndFeel1.LookAndFeel.SkinName = "Summer 2008";
}
private void TestSave()
{
//初始化业务逻辑
DataManager dataManager = new DataManager(DataProvider.Instance.CreateConnection());
DataSet data = null;//你要保存的数据
dataManager.Update(data);
}
private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
this.PrepareData(_CurrentBusiness);
bool result = _DataManager.Update(_CurrentBusiness, _UpdateType); //保存数据
if (result)
MessageBox.Show("牛人,更新完成!");
else
MessageBox.Show("更新失败!");
}
private void PrepareData(DataSet data)
{
DataTable master = data.Tables[0];
DataRow row1 = master.NewRow();
row1["TakeNo"] = txtTakeNo.Text;
row1["TakeDate"] = txtTakeDate.DateTime;
row1["CreateUser"] = txtUser.Text;
row1["Remark"] = txtRemark.Text;
master.Rows.Add(row1);
}
private void gcDetail_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
{
if (txtTakeNo.Text == "")
{
MessageBox.Show("请输入盘点单号!");
return;
}
DataTable dt = _CurrentBusiness.Tables[1];
DataRow row = dt.NewRow();
row["TakeNo"] = txtTakeNo.Text;
row["Qty"] = 0;
dt.Rows.Add(row);
gcDetail.RefreshDataSource();
gvDetail.MoveLast();
}
private void gvDetail_CellValueChanging(object sender,
DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
//如果更改产品编号的值,带出产品信息
if (e.Column == colProductCode)
{
//获取数据
DataRow productInfo = _DataManager.GetProductInfoByKey(e.Value.ToString());
//设置产品名称
gvDetail.SetFocusedRowCellValue(colProductName, productInfo["ProductName"]);
}
}
}
业务逻辑代码
/// <summary>
/// 业务逻辑管理主类
/// </summary>
public class DataManager : DataUpdate
{
public DataManager(OleDbConnection conn)
: base(conn)
{
//
}
protected override Type GetTypeByTableName(string tableName)
{
if (tableName == TProductTake.TableName)
return typeof(TProductTake);
if (tableName == TProductTakeDetail.TableName)
return typeof(TProductTakeDetail);
throw new Exception("没绑定数据表结构定义!");
}
public bool Update(System.Data.DataSet currentBusiness, UpdateType updateType)
{
return base.Update(currentBusiness);
}
/// <summary>
/// 取主/从表表结构,空数据
/// </summary>
/// <returns></returns>
public System.Data.DataSet BuildDemoData()
{
string sqlm = "select * from [tb_MyProductTake] where 1=0 ";
string sqld = "select * from [tb_MyProductTakeDetail] where 1=0 ";
DataSet ds = new DataSet();
ds.Tables.Add(DataProvider.Instance.GetDataTable(sqlm, TProductTake.TableName).Copy());
ds.Tables.Add(DataProvider.Instance.GetDataTable(sqld, TProductTakeDetail.TableName).Copy());
return ds;
}
public DataTable GetProductData()
{
string sql = "select * from tb_MyProduct";
return DataProvider.Instance.GetDataTable(sql, "tb_Product");
}
public DataRow GetProductInfoByKey(string productCode)
{
string sql = "select * from [tb_MyProduct] where [ProductCode]=’" + productCode + "’";
DataTable dt = DataProvider.Instance.GetDataTable(sql, "tb_Product");
if (dt.Rows.Count > 0)
return dt.Rows[0];
else
return null;
}
}
数据更新流程
本程序使用DevExpress for .net 9.2x版及Access数据库。
如将后台数据更新模型改成支持SQL数据库,替换vjsdn.UpdateFramework工程内所有"OleDb"
为"Sql"即可.
扫一扫加微信