上一节讲的是如何把单表映射成一个实体和一个数据表。
这里我们讲述如何实现一对多的多表关联,这种关联在开发中也是最常见的。
通常一对多的情况下,我们要求,主表的primarykey应该是 从表的primarykey之一。为了区分各个表之间的PrimaryKey,我们引入了SecondaryKey的概念。用主表PrimaryKey+从表SecondaryKey构建了一个从表的PrimaryKey.呵呵,这里说得有点拗口。所有的sql都是基于这个模式构建的。所以,我们以SendOutDataSet为例,我出售了两样东西,那么我数据库里面应该存放 卖给谁SendOut 卖了什么东西SendOutItem .主表SendOut《Primarykey="SendID"》,从表SendOutItem.<primarykey=SendID secondaryKey=ItemID>
这样我们就构建这样的一个DataSet包含了两个DataTable.这两个DataTable把我们设置的PrimaryKey SecondaryKey的属性写入Column 的ExtendsProperties中。我们只需要做这样的一个从这个DataSet获取这样信息并且构建相应的Insert/Update/Delete等等方法的DataAccess就可以实现不需要再写那些讨厌的Insert/Update/Delete语句了。啊,世界从此清净了许多。
然后,我们还发现了还有不足的地方,那就是很多时候我们要删除一整个的数据,比如我卖出去一个单子的东西很多很杂,如果我用DataSet的形式的话,那么我就变成要一条一条的删除,所以,我们就制造了UpdateObject DeleteObject来减轻这种负担,比如我想删除某个人的订单<~_~!当然只是假设>,那么我需要构建两个Delete对象,然后传递给DataAccess,来执行两次的Sql,这样我们依然只是需要代码生成器来生成,还是不需要手写Update/Delete语句,目的也很明显,为了防止新手误用了错误的Sql语句,结果导致数据被删除。虽然可以通过日志的方式恢复,但是也很麻烦。
当然了,DataAccess还应该作更多的事情,比如获取服务器时间,表之间的拷贝<表结构一致的情况下>,依据PrimaryKey删除数据,根据PrimaryKey获取<SendID传入,根据DataSet来获取SendOut SendOutItem的数据。>。这些事情都是一劳永逸的,一旦做好,就可以反复的重新利用,而且整个框架也就两三个DataAccess。但是有很多的DataSet BusinessRule BusinessFacade。
sample code:
using System;
using FishSky.Data.Base;
namespace FishSkyTest.DataTest
{
/**////<summary>
/// SendOutEntity实体类
///</summary>
public class SendOutEntity : BaseEntity
{
构造函数#region 构造函数
/**////<summary>
/// 构造函数
///</summary>
public SendOutEntity()
{
}
#endregion
出库单#region 出库单
private String mSendOutNo;
/**////<summary>
/// 出库单
///</summary>
public String SendOutNo
{
get { return mSendOutNo; }
set { mSendOutNo = value; }
}
#endregion
发票号#region 发票号
private String mInvoiceNo;
/**////<summary>
/// 发票号
///</summary>
public String InvoiceNo
{
get { return mInvoiceNo; }
set { mInvoiceNo = value; }
}
#endregion
总计#region 总计
private Decimal mAmount;
/**////<summary>
/// 总计
///</summary>
public Decimal Amount
{
get { return mAmount; }
set { mAmount = value; }
}
#endregion
操作员#region 操作员
private String mUserName;
/**////<summary>
/// 操作员
///</summary>
public String UserName
{
get { return mUserName; }
set { mUserName = value; }
}
#endregion
操作员中文名#region 操作员中文名
private String mChineseName;
/**////<summary>
/// 操作员中文名
///</summary>
public String ChineseName
{
get { return mChineseName; }
set { mChineseName = value; }
}
#endregion
客户代码#region 客户代码
private String mCustomerCode;
/**////<summary>
/// 客户代码
///</summary>
public String CustomerCode
{
get { return mCustomerCode; }
set { mCustomerCode = value; }
}
#endregion
客户中文名#region 客户中文名
private String mCustomerName;
/**////<summary>
/// 客户中文名
///</summary>
public String CustomerName
{
get { return mCustomerName; }
set { mCustomerName = value; }
}
#endregion
备注#region 备注
private String mRemark;
/**////<summary>
/// 备注
///</summary>
public String Remark
{
get { return mRemark; }
set { mRemark = value; }
}
#endregion
货款付讫#region 货款付讫
private String mIsPaid;
/**////<summary>
/// 货款付讫
///</summary>
public String IsPaid
{
get { return mIsPaid; }
set { mIsPaid = value; }
}
#endregion
出售日期#region 出售日期
private DateTime mSendOutDate;
/**////<summary>
/// 出售日期
///</summary>
public DateTime SendOutDate
{
get { return mSendOutDate; }
set { mSendOutDate = value; }
}
#endregion
}
}
using System;
using FishSky.Data.Base;
namespace FishSkyTest.DataTest
{
/**////<summary>
/// SendOutItemEntity实体类
///</summary>
public class SendOutItemEntity : BaseEntity
{
构造函数#region 构造函数
/**////<summary>
/// 构造函数
///</summary>
public SendOutItemEntity()
{
}
#endregion
送货单号#region 送货单号
private String mSendOutNo;
/**////<summary>
/// 送货单号
///</summary>
public String SendOutNo
{
get { return mSendOutNo; }
set { mSendOutNo = value; }
}
#endregion
项目编号#region 项目编号
private Int32 mItemNo;
/**////<summary>
/// 项目编号
///</summary>
public Int32 ItemNo
{
get { return mItemNo; }
set { mItemNo = value; }
}
#endregion
库存编号#region 库存编号
private String mStockID;
/**////<summary>
/// 库存编号
///</summary>
public String StockID
{
get { return mStockID; }
set { mStockID = value; }
}
#endregion
货物代码#region 货物代码
private String mMerchandiseCode;
/**////<summary>
/// 货物代码
///</summary>
public String MerchandiseCode
{
get { return mMerchandiseCode; }
set { mMerchandiseCode = value; }
}
#endregion
货物中文名#region 货物中文名
private String mChineseName;
/**////<summary>
/// 货物中文名
///</summary>
public String ChineseName
{
get { return mChineseName; }
set { mChineseName = value; }
}
#endregion
数量#region 数量
private Int32 mQuantity;
/**////<summary>
/// 数量
///</summary>
public Int32 Quantity
{
get { return mQuantity; }
set { mQuantity = value; }
}
#endregion
价格#region 价格
private Int64 mPrice;
/**////<summary>
/// 价格
///</summary>
public Int64 Price
{
get { return mPrice; }
set { mPrice = value; }
}
#endregion
进价#region 进价
private Int64 mInPrice;
/**////<summary>
/// 进价
///</summary>
public Int64 InPrice
{
get { return mInPrice; }
set { mInPrice = value; }
}
#endregion
备注#region 备注
private String mRemark;
/**////<summary>
/// 备注
///</summary>
public String Remark
{
get { return mRemark; }
set { mRemark = value; }
}
#endregion
}
}
using System;
using System.Data;
using FishSky.Data.Base;
namespace FishSkyTest.DataTest
{
public class SendOutDataTable : BaseDataTable
{
表格常量#region 表格常量
/**//// <summary>
/// 定义对象数据集表名:SendOut
/// </summary>
public const String TBL_SendOut = "SendOut";
/**//// <summary>
///出库单
/// </summary>
public const String SendOutNo_SendOut = "SendOutNo";
/**//// <summary>
///发票号
/// </summary>
public const String InvoiceNo_SendOut = "InvoiceNo";
/**//// <summary>
///总计
/// </summary>
public const String Amount_SendOut = "Amount";
/**//// <summary>
///操作员
/// </summary>
public const String UserName_SendOut = "UserName";
/**//// <summary>
///操作员中文名
/// </summary>
public const String ChineseName_SendOut = "ChineseName";
/**//// <summary>
///客户代码
/// </summary>
public const String CustomerCode_SendOut = "CustomerCode";
/**//// <summary>
///客户中文名
/// </summary>
public const String CustomerName_SendOut = "CustomerName";
/**//// <summary>
///备注
/// </summary>
public const String Remark_SendOut = "Remark";
/**//// <summary>
///货款付讫
/// </summary>
public const String IsPaid_SendOut = "IsPaid";
/**//// <summary>
///出售日期
/// </summary>
public const String SendOutDate_SendOut = "SendOutDate";
#endregion
构造函数#region 构造函数
/**////<summary>
/// 构造函数
///</summary>
public SendOutDataTable()
{
this.TableName = TBL_SendOut;
DataColumnCollection columns = this.Columns;
DataColumn column;
column = new DataColumn(SendOutNo_SendOut, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, true);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 50;
column.DefaultValue = " ";
columns.Add(column);
column = new DataColumn(InvoiceNo_SendOut, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 50;
column.DefaultValue = " ";
columns.Add(column);
column = new DataColumn(Amount_SendOut, typeof (Decimal));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.DefaultValue = 0;
columns.Add(column);
column = new DataColumn(UserName_SendOut, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 20;
column.DefaultValue = " ";
columns.Add(column);
column = new DataColumn(ChineseName_SendOut, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 50;
column.DefaultValue = " ";
columns.Add(column);
column = new DataColumn(CustomerCode_SendOut, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 20;
column.DefaultValue = " ";
columns.Add(column);
column = new DataColumn(CustomerName_SendOut, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 50;
column.DefaultValue = " ";
columns.Add(column);
column = new DataColumn(Remark_SendOut, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 100;
column.DefaultValue = " ";
columns.Add(column);
column = new DataColumn(IsPaid_SendOut, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 1;
column.DefaultValue = " ";
columns.Add(column);
column = new DataColumn(SendOutDate_SendOut, typeof (DateTime));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.DefaultValue = DateTime.Now;
columns.Add(column);
}
#endregion
创建实体#region 创建实体
/**////<summary>
/// 创建实体
///</summary>
///<returns>实体</returns>
public override BaseEntity CreateEntity()
{
return new SendOutEntity();
}
#endregion
获取指定行号的实体#region 获取指定行号的实体
/**////<summary>
/// 获取指定行号的实体
///</summary>
/// <param name="rowIndex">行号</param>
/// <returns>实体</returns>
public override BaseEntity GetEntity(int rowIndex)
{
if (rowIndex >= 0 && rowIndex <= this.Rows.Count)
{
SendOutEntity entity = new SendOutEntity();
entity.RowIndex = rowIndex;
DataRow dr = this.Rows[rowIndex];
entity.SendOutNo = dr[SendOutNo_SendOut].ToString();
entity.InvoiceNo = dr[InvoiceNo_SendOut].ToString();
entity.Amount = Convert.ToDecimal(dr[Amount_SendOut]);
entity.UserName = dr[UserName_SendOut].ToString();
entity.ChineseName = dr[ChineseName_SendOut].ToString();
entity.CustomerCode = dr[CustomerCode_SendOut].ToString();
entity.CustomerName = dr[CustomerName_SendOut].ToString();
entity.Remark = dr[Remark_SendOut].ToString();
entity.IsPaid = dr[IsPaid_SendOut].ToString();
entity.SendOutDate = Convert.ToDateTime(dr[SendOutDate_SendOut]);
return entity;
}
else
{
throw new ApplicationException("提示:您输入的行号有误!");
}
}
#endregion
设置实体值到DataTable#region 设置实体值到DataTable
/**////<summary>
/// 设置实体值到DataTable
///</summary>
///<param name="baseEntity">实体</param>
///<returns>是否设置成功</returns>
public override bool SetEntity(BaseEntity baseEntity)
{
int rowIndex = baseEntity.RowIndex;
SendOutEntity entity = baseEntity as SendOutEntity;
if (entity == null)
return false;
DataRow dr;
if (rowIndex == -1)
{
dr = this.NewRow();
dr[SendOutNo_SendOut] = entity.SendOutNo;
dr[InvoiceNo_SendOut] = entity.InvoiceNo;
dr[Amount_SendOut] = entity.Amount;
dr[UserName_SendOut] = entity.UserName;
dr[ChineseName_SendOut] = entity.ChineseName;
dr[CustomerCode_SendOut] = entity.CustomerCode;
dr[CustomerName_SendOut] = entity.CustomerName;
dr[Remark_SendOut] = entity.Remark;
dr[IsPaid_SendOut] = entity.IsPaid;
dr[SendOutDate_SendOut] = entity.SendOutDate;
this.Rows.Add(dr);
return true;
}
else
{
if (rowIndex >= 0 && rowIndex < this.Rows.Count)
{
try
{
dr = this.Rows[rowIndex];
//只要是Key的值都不可以更改
dr[InvoiceNo_SendOut] = entity.InvoiceNo;
dr[Amount_SendOut] = entity.Amount;
dr[UserName_SendOut] = entity.UserName;
dr[ChineseName_SendOut] = entity.ChineseName;
dr[CustomerCode_SendOut] = entity.CustomerCode;
dr[CustomerName_SendOut] = entity.CustomerName;
dr[Remark_SendOut] = entity.Remark;
dr[IsPaid_SendOut] = entity.IsPaid;
dr[SendOutDate_SendOut] = entity.SendOutDate;
return true;
}
catch
{
return false;
}
}
else
{
return false;
}
}
}
#endregion
}
}
using System;
using System.Data;
using FishSky.Data.Base;
namespace FishSkyTest.DataTest
{
public class SendOutItemDataTable : BaseDataTable
{
表格常量#region 表格常量
/**//// <summary>
/// 定义对象数据集表名:SendOutItem
/// </summary>
public const String TBL_SendOutItem = "SendOutItem";
/**//// <summary>
///送货单号
/// </summary>
public const String SendOutNo_SendOutItem = "SendOutNo";
/**//// <summary>
///项目编号
/// </summary>
public const String ItemNo_SendOutItem = "ItemNo";
/**//// <summary>
///库存编号
/// </summary>
public const String StockID_SendOutItem = "StockID";
/**//// <summary>
///货物代码
/// </summary>
public const String MerchandiseCode_SendOutItem = "MerchandiseCode";
/**//// <summary>
///货物中文名
/// </summary>
public const String ChineseName_SendOutItem = "ChineseName";
/**//// <summary>
///数量
/// </summary>
public const String Quantity_SendOutItem = "Quantity";
/**//// <summary>
///价格
/// </summary>
public const String Price_SendOutItem = "Price";
/**//// <summary>
///进价
/// </summary>
public const String InPrice_SendOutItem = "InPrice";
/**//// <summary>
///备注
/// </summary>
public const String Remark_SendOutItem = "Remark";
#endregion
构造函数#region 构造函数
/**////<summary>
/// 构造函数
///</summary>
public SendOutItemDataTable()
{
this.TableName = TBL_SendOutItem;
DataColumnCollection columns = this.Columns;
DataColumn column;
column = new DataColumn(SendOutNo_SendOutItem, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, true);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 20;
column.DefaultValue = " ";
columns.Add(column);
column = new DataColumn(ItemNo_SendOutItem, typeof (Int32));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, true);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.DefaultValue = 0;
columns.Add(column);
column = new DataColumn(StockID_SendOutItem, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 20;
column.DefaultValue = " ";
columns.Add(column);
column = new DataColumn(MerchandiseCode_SendOutItem, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 20;
column.DefaultValue = " ";
columns.Add(column);
column = new DataColumn(ChineseName_SendOutItem, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 50;
column.DefaultValue = " ";
columns.Add(column);
column = new DataColumn(Quantity_SendOutItem, typeof (Int32));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.DefaultValue = 0;
columns.Add(column);
column = new DataColumn(Price_SendOutItem, typeof (Int64));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.DefaultValue = 0;
columns.Add(column);
column = new DataColumn(InPrice_SendOutItem, typeof (Int64));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.DefaultValue = 0;
columns.Add(column);
column = new DataColumn(Remark_SendOutItem, typeof (String));
column.ExtendedProperties.Add(BaseConst.isPrimaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isSecondaryKeyColumn, false);
column.ExtendedProperties.Add(BaseConst.isValidColumn, true);
column.MaxLength = 100;
column.DefaultValue = " ";
columns.Add(column);
}
#endregion
创建实体#region 创建实体
/**////<summary>
/// 创建实体
///</summary>
///<returns>实体</returns>
public override BaseEntity CreateEntity()
{
return new SendOutItemEntity();
}
#endregion
获取指定行号的实体#region 获取指定行号的实体
/**////<summary>
/// 获取指定行号的实体
///</summary>
/// <param name="rowIndex">行号</param>
/// <returns>实体</returns>
public override BaseEntity GetEntity(int rowIndex)
{
if (rowIndex >= 0 && rowIndex <= this.Rows.Count)
{
SendOutItemEntity entity = new SendOutItemEntity();
entity.RowIndex = rowIndex;
DataRow dr = this.Rows[rowIndex];
entity.SendOutNo = dr[SendOutNo_SendOutItem].ToString();
entity.ItemNo = Convert.ToInt32(dr[ItemNo_SendOutItem]);
entity.StockID = dr[StockID_SendOutItem].ToString();
entity.MerchandiseCode = dr[MerchandiseCode_SendOutItem].ToString();
entity.ChineseName = dr[ChineseName_SendOutItem].ToString();
entity.Quantity = Convert.ToInt32(dr[Quantity_SendOutItem]);
entity.Price = Convert.ToInt64(dr[Price_SendOutItem]);
entity.InPrice = Convert.ToInt64(dr[InPrice_SendOutItem]);
entity.Remark = dr[Remark_SendOutItem].ToString();
return entity;
}
else
{
throw new ApplicationException("提示:您输入的行号有误!");
}
}
#endregion
设置实体值到DataTable#region 设置实体值到DataTable
/**////<summary>
/// 设置实体值到DataTable
///</summary>
///<param name="baseEntity">实体</param>
///<returns>是否设置成功</returns>
public override bool SetEntity(BaseEntity baseEntity)
{
int rowIndex = baseEntity.RowIndex;
SendOutItemEntity entity = baseEntity as SendOutItemEntity;
if (entity == null)
return false;
DataRow dr;
if (rowIndex == -1)
{
dr = this.NewRow();
dr[SendOutNo_SendOutItem] = entity.SendOutNo;
dr[ItemNo_SendOutItem] = entity.ItemNo;
dr[StockID_SendOutItem] = entity.StockID;
dr[MerchandiseCode_SendOutItem] = entity.MerchandiseCode;
dr[ChineseName_SendOutItem] = entity.ChineseName;
dr[Quantity_SendOutItem] = entity.Quantity;
dr[Price_SendOutItem] = entity.Price;
dr[InPrice_SendOutItem] = entity.InPrice;
dr[Remark_SendOutItem] = entity.Remark;
this.Rows.Add(dr);
return true;
}
else
{
if (rowIndex >= 0 && rowIndex < this.Rows.Count)
{
try
{
dr = this.Rows[rowIndex];
//只要是Key的值都不可以更改
dr[StockID_SendOutItem] = entity.StockID;
dr[MerchandiseCode_SendOutItem] = entity.MerchandiseCode;
dr[ChineseName_SendOutItem] = entity.ChineseName;
dr[Quantity_SendOutItem] = entity.Quantity;
dr[Price_SendOutItem] = entity.Price;
dr[InPrice_SendOutItem] = entity.InPrice;
dr[Remark_SendOutItem] = entity.Remark;
return true;
}
catch
{
return false;
}
}
else
{
return false;
}
}
}
#endregion
}
}
using System;
using System.Runtime.Serialization;
using FishSky.Data.Base;
namespace FishSkyTest.DataTest
{
[Serializable]
public class SendOutDataSet : BaseDataSet
{
表格常量及数据表#region 表格常量及数据表
/**//// <summary>
/// 定义对象数据集表:SendOutDataTable
/// </summary>
public SendOutDataTable SendOutTable = new SendOutDataTable();
/**//// <summary>
/// 定义对象数据集表:SendOutItemDataTable
/// </summary>
public SendOutItemDataTable SendOutItemTable = new SendOutItemDataTable();
#endregion
构造函数#region 构造函数
/**//// <summary>
/// 构造函数
/// </summary>
public SendOutDataSet() : base()
{
}
#endregion
序列化的构造函数#region 序列化的构造函数
/**//// <summary>
/// 序列化的构造函数
/// </summary>
public SendOutDataSet(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
#endregion
建立数据集的表格#region 建立数据集的表格
/**//// <summary>
/// 建立数据集的表格
/// </summary>
protected override void BuildDataTables()
{
this.Tables.Add(SendOutTable);
this.Tables.Add(SendOutItemTable);
//设置新增自动生成key字段
this.KeyTypeID = "SendOutNo";
}
#endregion
}
}
using System;
using System.Data;
using NUnit.Framework;
namespace FishSkyTest.DataTest
{
/**//// <summary>
/// SendOutDataSetTest 的摘要说明。----Multi-Table && Multi-Entity
/// </summary>
[TestFixture]
public class SendOutDataSetTest
{
private SendOutDataSet ds=new SendOutDataSet();
public SendOutDataSetTest()
{
}
[SetUp]
public void SetUp()
{
ds.SendOutTable.Rows.Clear();
ds.SendOutItemTable.Rows.Clear();
DataRow dr=ds.SendOutTable.NewRow();
dr[SendOutDataTable.Amount_SendOut]=300;
dr[SendOutDataTable.ChineseName_SendOut]="拉拉";
dr[SendOutDataTable.CustomerCode_SendOut]="aa";
dr[SendOutDataTable.CustomerName_SendOut]="asdfasdfasdf";
dr[SendOutDataTable.InvoiceNo_SendOut]="fapiaohao";
dr[SendOutDataTable.IsPaid_SendOut]=0;
dr[SendOutDataTable.Remark_SendOut]="remark";
dr[SendOutDataTable.SendOutDate_SendOut]=DateTime.Now;
dr[SendOutDataTable.SendOutNo_SendOut]="S001";
dr[SendOutDataTable.UserName_SendOut]="kevin";
ds.SendOutTable.Rows.Add(dr);
dr=ds.SendOutItemTable.NewRow();
dr[SendOutItemDataTable.ChineseName_SendOutItem]="lala vine";
dr[SendOutItemDataTable.InPrice_SendOutItem]=120;
dr[SendOutItemDataTable.ItemNo_SendOutItem]=1;
dr[SendOutItemDataTable.MerchandiseCode_SendOutItem]="lala";
dr[SendOutItemDataTable.Price_SendOutItem]=300;
dr[SendOutItemDataTable.Quantity_SendOutItem]=20;
dr[SendOutItemDataTable.Remark_SendOutItem]="remark";
dr[SendOutItemDataTable.SendOutNo_SendOutItem]="S001";
dr[SendOutItemDataTable.StockID_SendOutItem]="ST000001";
ds.SendOutItemTable.Rows.Add(dr);
}
[Test]
public void TestMultiNewEntity()
{
SetUp();
SendOutEntity outEntity=ds.SendOutTable.CreateEntity() as SendOutEntity;
outEntity.Amount=200;
outEntity.ChineseName="baasd";
outEntity.CustomerCode ="adfad";
outEntity.CustomerName ="seadifln";
outEntity.InvoiceNo ="Id932lnd";
outEntity.IsPaid ="N";
outEntity.Remark ="remarks";
outEntity.SendOutDate =DateTime.Now;
outEntity.SendOutNo ="S0002";
outEntity.UserName ="kevin";
ds.SendOutTable.SetEntity(outEntity);
SendOutItemEntity itemEntity =ds.SendOutItemTable.CreateEntity() as SendOutItemEntity;
itemEntity.ChineseName ="lala vines";
itemEntity.InPrice =120;
itemEntity.ItemNo=1;
itemEntity.MerchandiseCode ="lalades";
itemEntity.Price =200;
itemEntity.Quantity =200;
itemEntity.Remark ="adfad";
itemEntity.SendOutNo =outEntity.SendOutNo ;
itemEntity.StockID ="StkOutID";
ds.SendOutItemTable.SetEntity(itemEntity);
itemEntity =ds.SendOutItemTable.CreateEntity() as SendOutItemEntity;
itemEntity.ChineseName ="lala vines";
itemEntity.InPrice =120;
itemEntity.ItemNo=2;
itemEntity.MerchandiseCode ="galdiena";
itemEntity.Price =300;
itemEntity.Quantity =100;
itemEntity.Remark ="adfad";
itemEntity.SendOutNo =outEntity.SendOutNo ;
itemEntity.StockID ="StkOutID";
ds.SendOutItemTable.SetEntity(itemEntity);
Assert.AreEqual(ds.SendOutTable.Rows.Count,2) ;
Assert.AreEqual(ds.SendOutItemTable.Rows.Count,3) ;
Assert.IsFalse(ds.SendOutItemTable.SetEntity(outEntity));
}
}
}