**1、更新整个实体参数:**
~~~
UserTab user = new UserTab(){UserID= 1,UserName= "张三",Pwd="xxx"};
EntityQuery<UserTab>.Instance. Update (user);
~~~
这里是update表的所有字段,如果字段为NULL则不更新
注意:这种方式表必须要有主键。
**2、只更新某个字段:**
~~~
UserTab user = new UserTab(){UserID= 1,UserName= "张三",Pwd="xxx"};
OQL updateQ = OQL.From(user)
.Update(user.UserName) //仅仅更新姓名
.END;
EntityQuery<UserTab>.Instance.ExecuteOql(updateQ);
~~~
这里只更新UserName这个字段
**注意:这种方式没有指定where条件,所以表必须要有主键。**
所以以上的执行等同于:
`UPDATE [UserTab] SET [UserName] = '张三' Where [UserID] =1`
3、根据条件更新某些字段:
~~~
UPDATE [UserTab] SET [UserName] = '张三', [Pwd] = 'xxx' Where [UserID] =1
~~~
如果有多个这种相等的条件,可以都写在where里,如下:
`Where(user.UserID, user.UserName)`
以上也可以写成:
~~~
UserTab user = new UserTab() { UserName= "张三",Pwd="xxx" };
OQL updateQ = OQL.From(user)
.Update(user.UserName, user.Pwd)
.Where(cmp => cmp.Comparer(user.UserID, OQLCompare.CompareType.Equal, 1))
.END;
EntityQuery<UserTab>.Instance.ExecuteOql(updateQ);
~~~
如果条件比较复杂,则必须要用Comparer了,如下:
~~~
UserTab user = new UserTab() { UserName = "ssss1", UserTel = "333331" };
OQLCompareFunc<UserTab> cmpFun = (cmp, u) =>
{
OQLCompare cmpResult = null;
//and 条件
cmpResult = cmpResult & cmp.Comparer(user.UserName, OQLCompare.CompareType.Like, "%" + "x" + "%");
//or
cmpResult = cmpResult | cmp.Comparer(user.UserID, OQLCompare.CompareType.Greater, 1);
return cmpResult;
};
OQL update = OQL.From(user)
.Update(user.UserName, user.UserTel)
.Where(cmpFun)
.END;
EntityQuery<UserTab>.Instance.ExecuteOql(update);
~~~
这里定义的条件cmpFun可以多次调用。
也可以简化成下面的方式:
~~~
UserTab user = new UserTab() { UserName = "ssss000", UserTel = "33333000" };
OQL update = OQL.From(user)
.Update(user.UserName, user.UserTel)
.Where(cmp => cmp.Comparer(user.UserName, OQLCompare.CompareType.Like, "%" + "s" + "%")
& cmp.Comparer(user.UserSex, OQLCompare.CompareType.Equal, "男"))
.END;
EntityQuery<UserTab>.Instance.ExecuteOql(update);
~~~
4、UpdateSelf 函数(用于数字型字段的值加、减一个新值得情况,比如更新会员的积分,购买物品后将会员的原有积分加上本次获得的积分。)
~~~
UserTab user2 = new UserTab() { UserID = 1, StateFlag =10};
OQL qc = OQL.From(user2)
.UpdateSelf('+', user2.StateFlag)
.Where(user2.UserID)
.END;
EntityQuery<UserTab>.Instance.ExecuteOql(qc);
~~~
相对于的SQL语句为下面:
~~~
UPDATE [UserTab] SET [StateFlag]=[StateFlag] + 10 WHERE [UserID]=1
~~~