Gentle.Net学习笔记三:常用更新数据的方法总结
1.最简单的更新
private void btnUpdate_Click(object sender, System.EventArgs e)
{
UT_BM_COALMINE cm=UT_BM_COALMINE.Retrieve("1201010001");
cm.COALMINENAME="矿点A";
cm.Persist();
}
{
UT_BM_COALMINE cm=UT_BM_COALMINE.Retrieve("1201010001");
cm.COALMINENAME="矿点A";
cm.Persist();
}
2.如果想要改变数据的主键,就没有那么方便了,只能是先删除-然后改变状态-然后改变主键-最后再插入保存
private void btnUpdateKey_Click(object sender, System.EventArgs e)
{
try
{
UT_BM_COALMINE cm=UT_BM_COALMINE.Retrieve("1201010001");
cm.Remove();
cm.IsPersisted=false;
cm.COALMINEID="120101000a";
cm.Persist();
}
catch(GentleException ex)
{
if(ex.Error==Gentle.Common.Error.UnexpectedRowCount)
this.ShowMessage("要更新的数据并不存在!"+ex.Error.ToString ());
else
throw;
}
}
{
try
{
UT_BM_COALMINE cm=UT_BM_COALMINE.Retrieve("1201010001");
cm.Remove();
cm.IsPersisted=false;
cm.COALMINEID="120101000a";
cm.Persist();
}
catch(GentleException ex)
{
if(ex.Error==Gentle.Common.Error.UnexpectedRowCount)
this.ShowMessage("要更新的数据并不存在!"+ex.Error.ToString ());
else
throw;
}
}
3.当然,同查询一样,更新数据也可以直接使用sql语句进行
private void btnUpdateFree_Click(object sender, System.EventArgs e)
{
string sql="Update ut_bm_coalmine set coalmineid='1201010001' where coalmineid='120101000a'";
Broker.Execute(sql);
}
{
string sql="Update ut_bm_coalmine set coalmineid='1201010001' where coalmineid='120101000a'";
Broker.Execute(sql);
}