Advertisement
jwetoszka

mysql

Apr 1st, 2022 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. CREATE TABLE users (
  2. ID int NOT NULL AUTO_INCREMENT,
  3. LastName varchar(255) NOT NULL,
  4. FirstName varchar(255),
  5. Address varchar(255),
  6. City varchar(255),
  7. ZipCode varchar(255),
  8. Country varchar(255),
  9. Email varchar(255),
  10. AvatarURL varchar(255),
  11. PRIMARY KEY (ID)
  12. );
  13.  
  14.  
  15. {
  16. "ConnectionStrings": {
  17. "MySQL": "server=localhost;port=3306;user=xxx;password=xxx;database=xxx"
  18.  
  19. },
  20. "Logging": {
  21. "LogLevel": {
  22. "Default": "Information",
  23. "Microsoft": "Warning",
  24. "Microsoft.Hosting.Lifetime": "Information"
  25. }
  26. },
  27. "AllowedHosts": "*"
  28. }
  29.  
  30.  
  31.  
  32.  
  33. private readonly IConfiguration _configuration;
  34. public UserController(IConfiguration configuration)
  35. {
  36. _configuration = configuration;
  37. }
  38.  
  39.  
  40. [HttpGet]
  41. [Route("users")]
  42. public JsonResult Get()
  43. {
  44.  
  45. string query = @"select * from users";
  46. string sqlDataSource = _configuration.GetConnectionString("MySQL");
  47. DataTable table = new DataTable();
  48. MySqlDataReader myReader;
  49. using (MySqlConnection myCon = new MySqlConnection(sqlDataSource))
  50. {
  51. myCon.Open();
  52. using (MySqlCommand myCommand = new MySqlCommand(query, myCon))
  53. {
  54. myReader = myCommand.ExecuteReader();
  55. table.Load(myReader);
  56. myReader.Close();
  57. myCon.Close();
  58. }
  59. }
  60.  
  61. return new JsonResult(table);
  62. }
  63.  
  64.  
  65.  
  66. public void ConfigureServices(IServiceCollection services)
  67. {
  68. //Enable CORS
  69. services.AddCors(c =>
  70. {
  71. c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
  72. });
  73.  
  74. //JSON Serializer
  75. services.AddControllersWithViews().AddNewtonsoftJson(options =>
  76. options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
  77. .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver
  78. = new DefaultContractResolver());
  79.  
  80. services.AddControllers();
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement