日期:2014-05-18 浏览次数:20822 次
[Table(Name = "tbl_users")] public class User { #region Attributes [Column(Name = "uId", IsDbGenerated = true, IsPrimaryKey = true)] public int Id { get; set; } [Column(Name = "uName", CanBeNull = false)] public string Name { get; set; } #endregion }
DataContext con = new DataContext(Common.CONNSTR); con.GetTable<User>().InsertOnSubmit(new User() { Name = "王三" }); con.SubmitChanges(); con.Dispose(); 对应T-Sql: insert into tbl_users(uName) values('王三') try { return (new DataContext(Common.CONNSTR).GetTable<User>().Single(u => u.Id == 6 && u.Name == "王二")); } catch { return (null); } 对应T-Sql: select * from tbl_users where uId=6 and uName='王二'
DataContext con = new DataContext(Common.CONNSTR); con.GetTable<User>().Single(u => u.Id == 6 && u.Name == "王三").Name = "王二"; con.SubmitChanges(); con.Dispose(); 对应T-Sql: select * from tbl_users where uId=6 and uName='王三' update tbl_users set uName='王二' where uId=6 and uName='王三' DataContext con = new DataContext(Common.CONNSTR); con.GetTable<User>().DeleteOnSubmit(con.GetTable<User>().Single(u => u.Id == 6)); con.SubmitChanges(); con.Dispose(); 对应T-Sql: select * from tbl_users where uId=6 delete tbl_users where uId=6 and uName=(上一句查到的值)
修改: con.ExecuteCommand("update tbl_users set uName={0} where uId={1} and uName={2}", "王二", 6, "王三"); 删除: con.ExecuteCommand("delete tbl_users where uId={0}", 6);