private void LoadTableDemo() { DataTable dt = new DataTable(); dt.Columns.Add("Id", typeof(int)); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Age", typeof(string)); for (int i = 0; i < 3; i++) { int id = i + 1; DataRow row = dt.NewRow(); row["Id"] = id; row["Name"] = "Name " + id; row["Age"] = id * 10; dt.Rows.Add(row); } Table1.DataSource = dt; Table1.DataBind(); Table2.DataSource = dt; Table2.DataBind(); Table3.DataSource = dt; Table3.DataBind(); Table4.DataSource = dt; Table4.DataBind(); Table5.DataSource = dt; Table5.DataBind(); }
public ActionResult Delete() { Table4 model = new Table4() { Code = "ssdb" }; using (SqlConnection cn = new SqlConnection(DapperHelper.ConnStr)) { var r = cn.Delete(model); return(Content(r.ToString())); } }
public ActionResult Add() { using (SqlConnection cn = new SqlConnection(DapperHelper.ConnStr)) { Tabl2 model = new Tabl2() { AId = 1111, Name = "test" }; Table4 model2 = new Table4() { Code = "ssdb", Name = "test" }; var r = cn.Insert(model); cn.Insert(model2); return(Content(JsonConvert.SerializeObject(r))); } }
public void UpdateComplexField() { ForEachProvider(db => { var table = db.GetTable <Table4>(); int id = 3; try { var obj = new Table4(); obj.Object = new TestObject() { Value = 101 }; obj.Id = id = Convert.ToInt32(db.InsertWithIdentity(obj)); var obj2 = table.First(_ => _.Id == id); Assert.AreEqual(obj.Object.Value, obj2.Object.Value); obj.Object.Value = 999; db.Update(obj); obj2 = table.First(_ => _.Id == id); Assert.AreEqual(obj.Object.Value, obj2.Object.Value); obj.Object.Value = 666; table .Where(_ => _.Id == id) .Set(_ => _.Object, _ => obj.Object) .Update(); obj2 = table.First(_ => _.Id == id); Assert.AreEqual(obj.Object.Value, obj2.Object.Value); obj.Object.Value = 777; table .Where(_ => _.Id == id) .Set(_ => _.Object, obj.Object) .Update(); obj2 = table.First(_ => _.Id == id); Assert.AreEqual(obj.Object.Value, obj2.Object.Value); var id2 = Convert.ToInt32(table.InsertWithIdentity(() => new Table4 { Object = new TestObject() { Value = 300 } })); obj2 = table.First(_ => _.Id == id2); Assert.AreEqual(300, obj2.Object.Value); var id3 = Convert.ToInt32(table.Value(_ => _.Object, () => obj.Object) .InsertWithIdentity()); obj2 = table.First(_ => _.Id == id3); Assert.AreEqual(obj.Object.Value, obj2.Object.Value); var id4 = Convert.ToInt32(table.Value(_ => _.Object, obj.Object) .InsertWithIdentity()); obj2 = table.First(_ => _.Id == id4); Assert.AreEqual(obj.Object.Value, obj2.Object.Value); } finally { table.Delete(_ => _.Id >= id); } }); }
public void UpdateComplexField() { ForEachProvider(db => { var table = db.GetTable<Table4>(); int id = 3; try { var obj = new Table4(); obj.Object = new TestObject() {Value = 101}; obj.Id = id = Convert.ToInt32(db.InsertWithIdentity(obj)); var obj2 = table.First(_ => _.Id == id); Assert.AreEqual(obj.Object.Value, obj2.Object.Value); obj.Object.Value = 999; db.Update(obj); obj2 = table.First(_ => _.Id == id); Assert.AreEqual(obj.Object.Value, obj2.Object.Value); obj.Object.Value = 666; table .Where(_ => _.Id == id) .Set(_ => _.Object, _ => obj.Object) .Update(); obj2 = table.First(_ => _.Id == id); Assert.AreEqual(obj.Object.Value, obj2.Object.Value); obj.Object.Value = 777; table .Where(_ => _.Id == id) .Set(_ => _.Object, obj.Object) .Update(); obj2 = table.First(_ => _.Id == id); Assert.AreEqual(obj.Object.Value, obj2.Object.Value); var id2 = Convert.ToInt32(table.InsertWithIdentity(() => new Table4 { Object = new TestObject() {Value = 300} })); obj2 = table.First(_ => _.Id == id2); Assert.AreEqual(300, obj2.Object.Value); var id3 = Convert.ToInt32(table.Value(_ => _.Object, () => obj.Object) .InsertWithIdentity()); obj2 = table.First(_ => _.Id == id3); Assert.AreEqual(obj.Object.Value, obj2.Object.Value); var id4 = Convert.ToInt32(table.Value(_ => _.Object, obj.Object) .InsertWithIdentity()); obj2 = table.First(_ => _.Id == id4); Assert.AreEqual(obj.Object.Value, obj2.Object.Value); } finally { table.Delete(_ => _.Id >= id); } }); }