MySqlTest.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. namespace MySQLTool;
  2. public class MySqlTest
  3. {
  4. public void Test()
  5. {
  6. // 执行查询
  7. var queryResult = MySQLHelper.ExecuteQuery("SELECT * FROM your_table");
  8. foreach (var row in queryResult)
  9. {
  10. foreach (var pair in row)
  11. {
  12. Console.WriteLine($"{pair.Key}: {pair.Value}");
  13. }
  14. Console.WriteLine("-----------");
  15. }
  16. User user = new User { Name = "John Doe", Email = "john@example.com" };
  17. int rowsInserted = MySQLHelper.Insert("users", user);
  18. Console.WriteLine($"Rows inserted: {rowsInserted}");
  19. // 更新记录示例
  20. string updateCondition = "Name = 'John Doe'";
  21. User updatedUser = new User { Email = "new_email@example.com" };
  22. int rowsUpdated = MySQLHelper.Update("users", updatedUser, updateCondition);
  23. Console.WriteLine($"Rows updated: {rowsUpdated}");
  24. // 删除记录示例
  25. string deleteCondition = "Name = 'John Doe'";
  26. int rowsDeleted = MySQLHelper.Delete("users", deleteCondition);
  27. Console.WriteLine($"Rows deleted: {rowsDeleted}");
  28. // 执行反序列化操作示例
  29. var customObjectList = MySQLHelper.Query<User>("users");
  30. foreach (var item in customObjectList)
  31. {
  32. Console.WriteLine($"Name: {item.Name}, Email: {item.Email}");
  33. }
  34. }
  35. public class User
  36. {
  37. public string Name { get; set; }
  38. public string Email { get; set; }
  39. }
  40. }