C# 用于保存和删除按钮的CSLA实现代码
我是CSLA的新手,所以这里是我的问题。 我有一个简单的windows窗体应用程序,带有datagridview和两个按钮,分别是Save和Delete。我需要一个解决方案来实现Save()方法用于Save按钮,Delete()方法用于Delete按钮。我相信这很简单,但我不知道如何调用这两个方法。datagridview控件是可编辑的,所以我的想法是,当用户添加新行或编辑一些现有单元格并单击“保存”按钮时,这些数据将记录在我的数据库中。 提前谢谢你 我使用根可编辑集合和子可编辑原型。 下面是根目录的代码:C# 用于保存和删除按钮的CSLA实现代码,c#,csla,C#,Csla,我是CSLA的新手,所以这里是我的问题。 我有一个简单的windows窗体应用程序,带有datagridview和两个按钮,分别是Save和Delete。我需要一个解决方案来实现Save()方法用于Save按钮,Delete()方法用于Delete按钮。我相信这很简单,但我不知道如何调用这两个方法。datagridview控件是可编辑的,所以我的想法是,当用户添加新行或编辑一些现有单元格并单击“保存”按钮时,这些数据将记录在我的数据库中。 提前谢谢你 我使用根可编辑集合和子可编辑原型。 下面是根
[Serializable]
public class PDVCollection : BusinessBindingListBase<PDVCollection,PDV>
{
private PDVCollection()
{
AllowNew = true;
}
protected override object AddNewCore()
{
var item = PDV.PDV();
Add(item);
return item;
}
#region Factory Methods
public static PDVCollection GetAll()
{
return DataPortal.Fetch<PDVCollection>();
}
protected override void DataPortal_Update()
{
Child_Update();
}
#endregion
#region Data Access
private void DataPortal_Fetch()
{
RaiseListChangedEvents = false;
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbpos");
MySqlCommand cmd = new MySqlCommand("usp_PDVSelect", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
try
{
con.Open();
MySqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
var stopa = PDV.GetPDV(dr);
Add(stopa);
}
con.Close();
}
catch (Exception xcp)
{
throw xcp;
}
RaiseListChangedEvents = true;
}
}
#endregion
[可序列化]
公共类PDVCollection:BusinessBindingListBase
{
私人PDVCollection()
{
AllowNew=真;
}
受保护的覆盖对象AddNewCore()
{
var item=PDV.PDV();
增加(项目);
退货项目;
}
#区域工厂法
公共静态PDVCollection GetAll()
{
返回DataPortal.Fetch();
}
受保护的覆盖无效数据门户_更新()
{
Child_Update();
}
#端区
#区域数据访问
私有void DataPortal_Fetch()
{
RaiseListChangedEvents=false;
MySqlConnection con=newmysqlconnection(“服务器=localhost;用户id=root;密码=1234;persistsecurityinfo=True;数据库=dbpos”);
MySqlCommand cmd=新的MySqlCommand(“usp_PDVSelect”,con);
cmd.CommandType=System.Data.CommandType.StoredProcess;
尝试
{
con.Open();
MySqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
var stopa=PDV.GetPDV(dr);
添加(stopa);
}
con.Close();
}
捕获(异常xcp)
{
抛出xcp;
}
RaiseListChangedEvents=true;
}
}
#端区
下面是可编辑子对象的代码
[Serializable]
public class PDV : BusinessBase<PDV>
{
public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(c => c.Id);
public int Id
{
get { return GetProperty(IdProperty); }
private set { LoadProperty(IdProperty, value); }
}
public static readonly PropertyInfo<Guid> UidProperty = RegisterProperty<Guid>(c => c.Uid);
public Guid Uid
{
get { return GetProperty(UidProperty); }
private set { LoadProperty(UidProperty, value); }
}
public static readonly PropertyInfo<decimal> StopaProperty = RegisterProperty<decimal>(c => c.Stopa);
public decimal Stopa
{
get { return GetProperty(StopaProperty); }
set { SetProperty(StopaProperty, value); }
}
#region Factory Methods
internal static PDV NewPDV()
{
return DataPortal.CreateChild<PDV>();
}
internal static PDV GetPDV(MySqlDataReader dr)
{
return DataPortal.FetchChild<StopaPDV>(dr);
}
private StopaPDV()
{
}
#endregion
#region DataAccess
protected override void Child_Create()
{
LoadProperty(UidProperty, Guid.NewGuid());
base.Child_Create();
}
private void Child_Fetch(MySqlDataReader dr)
{
LoadProperty(IdProperty,Int32.Parse(dr[0].ToString()));
LoadProperty(UidProperty, Guid.Parse(dr[1].ToString()));
LoadProperty(StopaProperty, Decimal.Parse(dr[2].ToString()));
}
private void Child_Insert()
{
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbpos");
MySqlCommand cmd = new MySqlCommand("usp_PDVInsert", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("_uid", MySqlDbType.VarChar, 36).Value = Uid;
cmd.Parameters.Add("_stopa", MySqlDbType.Decimal).Value = Stopa;
cmd.Parameters.Add("_id", MySqlDbType.Int32).Direction = System.Data.ParameterDirection.Output;
int ID = 0;
try
{
con.Open();
cmd.ExecuteNonQuery();
ID = Convert.ToInt32(cmd.Parameters["_id"].Value);
con.Close();
}
catch (Exception xcp)
{
throw xcp;
}
}
private void Child_Update()
{
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbpos");
MySqlCommand cmd = new MySqlCommand("usp_PDVUpdate", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("_stopa",MySqlDbType.Decimal).Value = Stopa;
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception xcp)
{
throw xcp;
}
}
private void Child_DeleteSelf()
{
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbpos");
MySqlCommand cmd = new MySqlCommand("usp_PDVDeleteById", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("_id", MySqlDbType.Int32).Value = Id;
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception xcp)
{
throw xcp;
}
}
#endregion
}
[可序列化]
公共类PDV:BusinessBase
{
公共静态只读属性info IdProperty=RegisterProperty(c=>c.Id);
公共整数Id
{
获取{return GetProperty(IdProperty);}
私有集{LoadProperty(IdProperty,value);}
}
公共静态只读属性INFO UidProperty=RegisterProperty(c=>c.Uid);
公共Guid Uid
{
获取{return GetProperty(UidProperty);}
私有集{LoadProperty(UidProperty,value);}
}
公共静态只读属性info StopaProperty=RegisterProperty(c=>c.Stopa);
公共十进制Stopa
{
获取{return GetProperty(StopaProperty);}
set{SetProperty(StopaProperty,value);}
}
#区域工厂法
内部静态PDV NewPDV()
{
返回DataPortal.CreateChild();
}
内部静态PDV GetPDV(MySqlDataReader dr)
{
返回DataPortal.FetchChild(dr);
}
私有StopaPDV()
{
}
#端区
#区域数据访问
受保护的覆盖无效子项_Create()
{
LoadProperty(UidProperty,Guid.NewGuid());
base.Child_Create();
}
私有void子对象获取(MySqlDataReader dr)
{
LoadProperty(IdProperty,Int32.Parse(dr[0].ToString());
LoadProperty(UidProperty,Guid.Parse(dr[1].ToString());
LoadProperty(StopaProperty,Decimal.Parse(dr[2].ToString());
}
私有无效子项插入()
{
MySqlConnection con=newmysqlconnection(“服务器=localhost;用户id=root;密码=1234;persistsecurityinfo=True;数据库=dbpos”);
MySqlCommand cmd=新的MySqlCommand(“usp_PDVInsert”,con);
cmd.CommandType=System.Data.CommandType.StoredProcess;
cmd.Parameters.Add(“\uuid”,MySqlDbType.VarChar,36).Value=uid;
cmd.Parameters.Add(“\u stopa”,MySqlDbType.Decimal)。Value=stopa;
cmd.Parameters.Add(“_id”,MySqlDbType.Int32).Direction=System.Data.ParameterDirection.Output;
int ID=0;
尝试
{
con.Open();
cmd.ExecuteNonQuery();
ID=Convert.ToInt32(cmd.Parameters[“\u ID”].Value);
con.Close();
}
捕获(异常xcp)
{
抛出xcp;
}
}
私有无效子对象更新()
{
MySqlConnection con=newmysqlconnection(“服务器=localhost;用户id=root;密码=1234;persistsecurityinfo=True;数据库=dbpos”);
MySqlCommand cmd=新的MySqlCommand(“usp_PDVUpdate”,con);
cmd.CommandType=System.Data.CommandType.StoredProcess;
cmd.Parameters.Add(“\u stopa”,MySqlDbType.Decimal)。Value=stopa;
尝试
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
捕获(异常xcp)
{
抛出xcp;
}
}
私有无效子对象_DeleteSelf()
{
MySqlConnection con=newmysqlconnection(“服务器=localhost;用户id=root;密码=1234;persistsecurityinfo=True;数据库=dbpos”);
MySqlCommand cmd=新的MySqlCommand(“usp_PDVDeleteById”,con);
cmd.CommandType=System.Data.CommandType.StoredProcess;
cmd.Parameters.Add(“\u id”,MySqlDbType.Int32).Value=id;
尝试
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
捕获(异常xcp)
{
抛出xcp;
}
}
#端区
}
如果我理解正确,您正在寻求有关如何实现Save and Delete按钮事件处理程序的帮助,该事件处理程序将对您的CSLA可编辑根列表/集合起作用,从而在业务对象中保存或删除数据
假设这确实是您所追求的,那么下面的内容应该会有所帮助
拯救
在Save按钮事件处理程序中,只需在PDVCollection
实例上调用Save()
实例方法
删除
至于Delete按钮事件处理程序,您需要知道要删除集合中的哪个项
public partial class MainForm : Form
{
private BusinessLogic.PDVCollection _pdvColllection;
private BusinessLogic.PDV _pdvCurrentlySelected;
public MainForm()
{
InitializeComponent();
// Keep a reference to the editable root collection
_pdvColllection = BusinessLogic.PDVCollection.GetList();
// Initialise the BindingSource with data. The DataGridView is connected to the BindingSource
pdvCollectionBindingSource.DataSource = _pdvColllection;
}
private void pdvCollectionBindingSource_CurrentChanged( object sender, EventArgs e )
{
// Update a local reference with the currently selected item in the binding source (set by the DataGridView)
_pdvCurrentlySelected = pdvCollectionBindingSource.Current as BusinessLogic.PDV;
}
private void saveButton_Click( object sender, EventArgs e )
{
// Save the root collection - this will update all of the items in the collection.
// - Note a new root collection is returned.
if ( !_pdvColllection.IsValid )
{
MessageBox.Show( "Cannot save list because some items are invalid", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information );
return;
}
_pdvColllection = _pdvColllection.Save();
// Update the binding source with the new instance;
pdvCollectionBindingSource.DataSource = null;
pdvCollectionBindingSource.DataSource = _pdvColllection;
}
private void deleteButton_Click( object sender, EventArgs e )
{
// Delete requires a PDV instance to be selected in the DataGridView
if ( _pdvCurrentlySelected == null )
{
MessageBox.Show( "Item to delete not selected", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information );
return;
}
_pdvColllection.Remove( _pdvCurrentlySelected );
// Depending on whether you want to save this delete to the background immediately, or leave it up to the user you may want to
// Save the root collection next:
saveButton.PerformClick();
}
}