jwetoszka

C#

Oct 25th, 2019 (edited)
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. {
  2.   "ConnectionStrings": {
  3.     "MySQL": "server=localhost;port=3307;user=root;password=root;database=JarekDB"
  4.  
  5.   },
  6.   "Logging": {
  7.     "LogLevel": {
  8.       "Default": "Information",
  9.       "Microsoft": "Warning",
  10.       "Microsoft.Hosting.Lifetime": "Information"
  11.     }
  12.   },
  13.   "AllowedHosts": "*"
  14. }
  15.  
  16.  
  17.  
  18.  
  19.            //Enable CORS
  20.             services.AddCors(c =>
  21.             {
  22.                 c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
  23.             });
  24.  
  25.             //JSON Serializer
  26.             services.AddControllersWithViews().AddNewtonsoftJson(options =>
  27.             options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
  28.                 .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver
  29.                 = new DefaultContractResolver());
  30.  
  31.  
  32.  
  33. using Microsoft.AspNetCore.Http;
  34. using Microsoft.AspNetCore.Mvc;
  35. using Microsoft.Extensions.Configuration;
  36. using MySqlConnector;
  37. using System;
  38. using System.Collections.Generic;
  39. using System.Data;
  40. using System.Linq;
  41. using System.Threading.Tasks;
  42.  
  43. namespace MySqlWebApi.Controllers
  44. {
  45.     [Route("api/[controller]")]
  46.     [ApiController]
  47.     public class UserController : ControllerBase
  48.     {
  49.         private readonly IConfiguration _configuration;
  50.         public UserController(IConfiguration configuration)
  51.         {
  52.             _configuration = configuration;
  53.         }
  54.  
  55.  
  56.         [HttpGet]
  57.         public JsonResult Get()
  58.         {
  59.  
  60.             string query = @"select * from  users";
  61.             string sqlDataSource = _configuration.GetConnectionString("MySQL");
  62.             DataTable table = new DataTable();
  63.             MySqlDataReader myReader;
  64.             using (MySqlConnection myCon = new MySqlConnection(sqlDataSource))
  65.             {
  66.                 myCon.Open();
  67.                 using (MySqlCommand myCommand = new MySqlCommand(query, myCon))
  68.                 {
  69.                     myReader = myCommand.ExecuteReader();
  70.                     table.Load(myReader);
  71.                     myReader.Close();
  72.                     myCon.Close();
  73.                 }
  74.             }
  75.  
  76.             return new JsonResult(table);
  77.         }
  78.  
  79.  
  80.  
  81.         [HttpGet("{id}")]
  82.         public JsonResult GetUser(int id)
  83.         {
  84.  
  85.             string query = @"select * from  users where id="+id;
  86.             string sqlDataSource = _configuration.GetConnectionString("MySQL");
  87.  
  88.             DataTable table = new DataTable();
  89.  
  90.             MySqlDataReader myReader;
  91.             using (MySqlConnection myCon = new MySqlConnection(sqlDataSource))
  92.             {
  93.                 myCon.Open();
  94.                 using (MySqlCommand myCommand = new MySqlCommand(query, myCon))
  95.                 {
  96.                     myReader = myCommand.ExecuteReader();
  97.                     table.Load(myReader);
  98.                     myReader.Close();
  99.                     myCon.Close();
  100.                 }
  101.             }
  102.  
  103.             return new JsonResult(table);
  104.         }
  105.     }
  106.  
  107.  
  108.  
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. class Samochod
  123.     {
  124.         public string marka;
  125.         public int predkosc;
  126.  
  127.         public void Przyspiesz(int a)
  128.         {
  129.             predkosc += a;
  130.         }
  131.  
  132.         public static Samochod operator + (Samochod s1, Samochod s2)
  133.         {
  134.             Samochod s3 = new Samochod();
  135.         ...
  136.         ...
  137.  
  138.  
  139.             return s3;
  140.         }
  141. ...
  142. ...
  143.  
  144.  
Add Comment
Please, Sign In to add comment