在我們的樣本中Account是一個單表的操作。
前面的隨筆已經有產生了AccountDataSet AccountDataTable AccountEntity AccountRule AccountSystem類了。
所以這裡我就不重複累贅了。
如果不想做,可以直接下載demo的代碼
我們建立一個Winform,取名TestSingleTable.cs
並如下畫好介面。
這裡我偷懶,直接把AccountDataSet拉到螢幕作為螢幕的gridview的資料來源
1.using上添加
using Wildfish.Data.ISeries;
using Wildfish.BusinessFacade.ISeries;
using Wildfish.SystemFrameWork.Base;
using Wildfish.SystemFrameWork.Utility;
2.在Form_Load的時候,我們擷取所有的Account資料ACCOUNTSystem mgrSystem=new ACCOUNTSystem();
ACCOUNTDataSet ds = mgrSystem.FillDataSetByAll();
this.gridAccount.DataSource = ds.ACCOUNTTable.DefaultView;
3.Add/Update的代碼如下if (this.txtName.Text.Trim().Length == 0)
{
this.txtName.Focus();
MessageBox.Show("Please input the name!");
return;
}
if (this.txtPassword.Text.Trim().Length == 0)
{
this.txtPassword.Focus();
MessageBox.Show("Please input the password!");
return;
}
if (this.txtNickname.Text.Trim().Length == 0)
{
this.txtNickname.Focus();
MessageBox.Show("Please input the nickname!");
return;
}
ACCOUNTSystem mgrSystem = new ACCOUNTSystem();
if (opMode == OperationMode.Add)
{
//check exist
if (mgrSystem.CheckExist(this.txtName.Text))
{
this.txtName.Focus();
MessageBox.Show("The name is already existed!");
return;
}
ACCOUNTDataSet ds=new ACCOUNTDataSet();
ACCOUNTEntity entity = ds.ACCOUNTTable.CreateEntity();
entity.NAME = this.txtName.Text;
entity.PASSWORD = SecurityUtility.Enctype(this.txtPassword.Text);
entity.NICKNAME = this.txtNickname.Text;
//ds.SetEntity(entity); or
//ds.ACCOUNTTable.SetEntity(entity);
ds.ACCOUNTTable.SetEntity(entity);
if (!mgrSystem.InsertData(ds))
{
MessageBox.Show("Error occur at insert the data to database!Please check the Wildfish.log.txt for detail!");
}
}
else
{
ACCOUNTDataSet ds = mgrSystem.FillDataSetByID(this.txtName.Text);
ACCOUNTEntity entity = ds.ACCOUNTTable.GetEntity(0);
entity.PASSWORD = SecurityUtility.Enctype(this.txtPassword.Text.Trim());
entity.NICKNAME = this.txtNickname.Text;
//ds.SetEntity(entity); or
//ds.ACCOUNTTable.SetEntity(entity);
ds.ACCOUNTTable.SetEntity(entity);
if (!mgrSystem.UpdateData(ds))
{
MessageBox.Show("Error occur at update the data to database!Please check the Wildfish.log.txt for detail!");
}
}
this.opMode = OperationMode.Add;
this.txtName.Enabled = true;
this.txtName.Text = "";
this.txtPassword.Enabled = true;
this.txtPassword.Text = "";
this.txtNickname.Enabled = true;
this.txtNickname.Text = "";
GetAllTheData();
這裡要講述的是如何使用Wildfish進行新增資料
首先,我們new一個外觀層對象 ACCOUNTSystem mgrSystem = new ACCOUNTSystem();
如果是新增
if (mgrSystem.CheckExist(this.txtName.Text))
用這個來判斷MainKey=Name的對應值的資料是否已經存在
AccountDataSet來產生一個AccountEntity
用AccountEntity的形式來設定值,會有類型校正,也可以在屬性設定處增加自己的校正代碼
設定完了之後,用AccountDataSet的執行個體SetEntity把Entity值設定到DataRow
最後調用外觀層對象的InsertData方法來達到新增。
如果是修改的話
ACCOUNTDataSet ds = mgrSystem.FillDataSetByID(this.txtName.Text);
ACCOUNTEntity entity = ds.ACCOUNTTable.GetEntity(0);
先擷取資料,如果剛才的資料儲存了,也可以用剛才的那個資料集
然後擷取實體
設定實體的值
調用ds.AccountDataTable.SetEntity();
最後調用外觀層對象的UpdateData方法來達到修改。
而Delete一條資料
擷取資料
打上刪除標誌,可以用datarow.delete,也可以用entity.delete,然後SetEntity的形式
然後調用外觀層對象的DeleteData方法來達到刪除。