前面的随笔已经有生成了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的代码如下ACCOUNTDataSet ds = mgrSystem.FillDataSetByAll();
this.gridAccount.DataSource = ds.ACCOUNTTable.DefaultView;
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进行新增数据{
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();
首先,我们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方法来达到删除。