Command各操作示例
例表 T_User ,结构如下:
ID |
PK, bigint |
自增主键 |
varchar(32) |
用户名 |
|
City |
varchar(32) |
所在城市 |
datetime |
注册时间 |
|
GroupID |
bigint |
用户组ID,为T_UserGroup表的外键 |
例表 T_UserGroup ,结构如下:
ID |
PK, bigint |
|
varchar(32) |
组名称 |
插入操作
var t = new T_User();
using(Command c = new Command()){
c.InsertInto(t);
c.Values(t.City, "杭州");
c.Values(t.UserName, "David");
c.Values(t.RegTime, SqlConst.GetDate); // 此处调用了SQLServer的系统日期函数getdate
// c.Values(t.RegTime, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); // 或者直接指定值
long newUserID = c.ExecuteWithIdentityReturn(); // 如果目标表没有自增主键,也可以直接用Execute方法
if(newUserID > 0){
// Success!
}else{
// Fail.
}
}
修改操作
var t = new T_User();
using(Command c = new Command()){
c.Update(t);
c.Set(t.City, "上海");
c.Where(t.UserName == "David");
if(c.Execute() > 0){
// Success!
}else{
// Fail.
}
}
删除操作
var t = new T_User();
using(Command c = new Command()){
c.Delete(t);
c.Where(t.UserName == "David");
if(c.Execute() > 0){
// Success!
}else{
// Fail.
}
}
