/// <summary> /// Dapper CURD /// </summary> static void TestDapper() { DapperHelper.ConnectionString = SQLServerHelper.GetConnectionStringByConfig(); Boolean b1 = DapperHelper.Insert(new User { UserName = "******", PassWord = "******" }); Boolean b2 = DapperHelper.Insert(Enumerable.Range(1, 10).Select(i => new User { UserName = "******" + i.ToString(), PassWord = "******" })); Boolean b3 = DapperHelper.Update(new User { UserName = "******", PassWord = "******" }); Boolean b4 = DapperHelper.Delete(new User { UserName = "******" }); var users = DapperHelper.Query(new User { UserName = "******" }); foreach (var user in users) { Console.WriteLine(user); } }
/// <summary> /// SqlServer增删改查 /// </summary> static void TestMSSql() { String username = "******"; String password = "******"; UserTable userTable = new UserTable(); Int32 i = 0; for (i = 1; i < Int32.MaxValue; i++) { if (!userTable.IsExist(username + i.ToString())) { break; } } User user1 = new User { UserName = username + i.ToString(), PassWord = password }; Console.WriteLine("{0} insert {1}", user1, userTable.Insert(user1)); User user2 = new User { UserName = username + (i + 1).ToString(), PassWord = password }; Console.WriteLine("{0} insert {1}", user2, userTable.InsertSafe(user2)); SQLServerHelper.ConnectionString = SQLServerHelper.GetConnectionStringByConfig(); // 插入 Boolean b = false; User user = new User { UserName = username + (i + 2).ToString(), PassWord = "******" }; b = SQLServerHelper.Insert(user); // 查询所有 List <User> list = SQLServerHelper.QueryAll <User>(); // 获取刚插入的一条 user = SQLServerHelper.QueryById <User, Int32>(list.Count); user.PassWord = "******"; // 更新 //b = SQLServerHelper.Update(user); // !!! b = SQLServerHelper.Update <User, Int32>(user, list.Count); // 查询 user = SQLServerHelper.QueryById <User, Int32>(list.Count); UserPo userPo = new UserPo(user); user = SQLServerHelper.QueryById <User, Int32>(list.Count, SQLServerHelper.nameof(() => userPo.Id)); // 删除 //b = SQLServerHelper.Delete(user); // !!! b = SQLServerHelper.Delete <User, Int32>(list.Count); list = SQLServerHelper.QueryAll <User>(); }