public void EntityInsertTest2() { using (Db db = Northwind.GetDb()) { using (DbConnectionInfo cn = db.CreateConnection()) { using (DbCommand cm = cn.CreateCommand()) { DeleteRecords(cm, entityIdStart2); } } KeyTableNextIdProvider nextId = (KeyTableNextIdProvider)db.NextIdProvider; nextId.CacheSize = recordsCount; DbTable table = db.GetDbTable(typeof(TestEntity)); db.SetTableAuditType(table.TableName, TableAuditTypes.None); DateTime startTime = DateTime.Now; using (DbTransactionInfo tr = db.BeginTransaction()) { int count = entityIdStart2 + recordsCount; for (int i = entityIdStart2; i < count; i++) { TestEntity te = new TestEntity(); te.Id = i; te.LoginName = "name" + i; te.CreatedOn = DateTime.Now; te.CreatedBy = "admin"; db.Insert<TestEntity>(te); } tr.Commit(); } DateTime endTime = DateTime.Now; Assert.Inconclusive("{0} seconds.", (endTime - startTime).TotalSeconds); } }
public void InsertTest() { int r; using (Db db = Northwind.GetDb()) { Db.AuditType = DbAuditTypes.Implicit; TestEntity u = new TestEntity() { Id = 1, LoginName = "wickyhu", Passwd = "888", Status = Status.Cancelled }; u.Photo = File.ReadAllBytes(Path.Combine(Environment.SystemDirectory, "user32.dll")); u.Text = File.ReadAllText(Path.Combine(Environment.SystemDirectory, "eula.txt"));//.Substring(0, 4000); u.Guid = Guid.NewGuid(); Address a = new Address("street", "city", "region", "postcode"); u.Address = a; using (DbTransactionInfo tr = db.BeginTransaction()) { r = db.Delete<TestEntity>(u); //r = db.Delete<TestEntity>(q => q.Id == 1 && q.LoginName == "wickyhu"); r = db.Insert<TestEntity>(u); Assert.AreEqual(r, 1); u.Passwd = "999"; u.Status = Status.NotApproved; r = db.Update<TestEntity>(u); Assert.AreEqual(r, 1); tr.Commit(); } TestEntity u3 = new TestEntity() { Id = 2, LoginName = "blabla2", Passwd = "9992" }; using (DbTransactionInfo tr = db.BeginTransaction()) { r = db.Delete<TestEntity>(q => q.Id == 2 && q.LoginName == "blabla2"); r = db.Insert<TestEntity>(u3); Assert.AreEqual(r, 1); tr.Commit(); } u3 = new TestEntity() { Id = 3, LoginName = "blabla3", Passwd = "9993" }; using (DbTransactionInfo tr = db.BeginTransaction()) { r = db.Insert<TestEntity>(u3); Assert.AreEqual(r, 1); tr.Rollback(); } TestEntity u2 = db.GetById<TestEntity>(1); Assert.AreEqual(u.Photo.Length, u2.Photo.Length); Assert.AreEqual(u.Text, u2.Text); //Assert.AreEqual(u.Guid, u2.Guid); Assert.AreEqual("wickyhu", u2.LoginName); Assert.AreEqual(Status.NotApproved, u2.Status); Assert.AreEqual("999", u2.Passwd); Assert.IsNotNull(u2.CreatedOn); Assert.IsNotNull(u2.CreatedBy); Assert.IsNotNull(u2.UpdatedBy); Assert.IsNotNull(u2.UpdatedOn); u3 = db.GetById<TestEntity>(3); Assert.IsNull(u3); } }
public void EntityInsertTest1() { using (Db db = Northwind.GetDb()) { using (DbConnectionInfo cn = db.CreateConnection()) { using (DbCommand cm = cn.CreateCommand()) { DeleteRecords(cm, entityIdStart1); } } DbTable table = db.GetDbTable(typeof(TestEntity)); db.SetTableAuditType(table.TableName, TableAuditTypes.None); int id = entityIdStart1; DateTime startTime = DateTime.Now; using (DbTransactionInfo tr = db.BeginTransaction()) { TestEntity te = new TestEntity(); te.Id = id; te.LoginName = "name" + id; te.CreatedOn = DateTime.Now; te.CreatedBy = "admin"; db.Insert<TestEntity>(te); tr.Commit(); } DateTime endTime = DateTime.Now; id++; db.SetTableAuditType(table.TableName, TableAuditTypes.SharedTable); DateTime startTime2 = DateTime.Now; using (DbTransactionInfo tr = db.BeginTransaction()) { TestEntity te = new TestEntity(); te.Id = id; te.LoginName = "name" + id; te.CreatedOn = DateTime.Now; te.CreatedBy = "admin"; db.Insert<TestEntity>(te); tr.Commit(); } DateTime endTime2 = DateTime.Now; id++; db.SetTableAuditType(table.TableName, TableAuditTypes.StandaloneTable); DateTime startTime3 = DateTime.Now; using (DbTransactionInfo tr = db.BeginTransaction()) { TestEntity te = new TestEntity(); te.Id = id; te.LoginName = "name" + id; te.CreatedOn = DateTime.Now; te.CreatedBy = "admin"; db.Insert<TestEntity>(te); tr.Commit(); } DateTime endTime3 = DateTime.Now; Assert.Inconclusive("No Audit: {0} seconds, Shared: {1} seconds, Standalone: {2} seconds", (endTime - startTime).TotalSeconds, (endTime2 - startTime2).TotalSeconds, (endTime3 - startTime3).TotalSeconds ); } }
public void SampleCodes() { //entity samples using (Db db = Northwind.GetDb()) { //insert using (DbTransactionInfo tr = db.BeginTransaction()) { TestEntity te = new TestEntity(); te.LoginName = "Admin"; db.Insert<TestEntity>(te); tr.Commit(); } //update using (DbTransactionInfo tr = db.BeginTransaction()) { var q = from e in db.Query<TestEntity>() where e.LoginName == "Admin" select e; TestEntity te = q.First(); te.LoginName = "Admin2"; db.Update<TestEntity>(te); //db.Delete<TestEntity>(te); tr.Commit(); } //delete using (DbTransactionInfo tr = db.BeginTransaction()) { db.Delete<TestEntity>(e => e.LoginName == "Admin2"); tr.Commit(); } } //ado samples using (Db db = Northwind.GetDb()) { using (DbTransactionInfo tr = db.BeginTransaction()) { using (DbCommand cm = tr.CreateCommand()) { string sql = String.Format(@"insert into orders (CustomerID,EmployeeID,OrderDate) values ({0},{1},{2})", db.GetParameterMarker("customerID"), db.GetParameterMarker("employeeID"), db.GetParameterMarker("orderDate") ); cm.CommandText = sql; cm.Parameters.Add(db.CreateParameter("customerID", "ALFKI")); cm.Parameters.Add(db.CreateParameter("employeeID", 1)); cm.Parameters.Add(db.CreateParameter("orderDate", DateTime.Today.AddYears(1))); db.ExecNonQuery(cm); tr.Commit(); } using (DbCommand cm = tr.CreateCommand()) { string sql = String.Format(@"select * from customers where CustomerID={0}", db.GetParameterMarker("customerID") ); cm.CommandText = sql; cm.Parameters.Add(db.CreateParameter("customerID", "ALFKI")); DataTable dt = db.ExecQuery(cm); } } } }