Advertisement
M0n5t3r

mysql-VS

Jan 6th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data.SqlClient;
  7. using MySql.Data;
  8. using System.Data;
  9. using MySql.Data.MySqlClient;
  10. namespace Proftaak_Controller
  11. {
  12.     class data
  13.     {
  14.         public static MySqlConnection con = new MySqlConnection(@"Server=localhost; Database=DATBASENAAM; Uid=; Pwd=;");
  15.         public static MySqlCommand cmd;
  16.         public static MySqlDataReader reader;
  17.         public void ConsoleAllUsers()
  18.         {
  19.             con.Close();
  20.             con.Open();
  21.             cmd = new MySqlCommand("SELECT * FROM Users", con);
  22.             reader = cmd.ExecuteReader();
  23.             while (reader.Read())
  24.             {
  25.                 foreach (Object ob in reader)
  26.                 {
  27.                     Console.WriteLine("id = " + reader[0]);
  28.                     Console.WriteLine("Naam = " + reader[1]);
  29.                 }
  30.             }
  31.             con.Close();
  32.         }
  33.  
  34.         public User getUser(string _kaartID)
  35.         {
  36.             User scannedUser = null;
  37.             con.Close();
  38.             con.Open();
  39.             cmd = new MySqlCommand("SELECT * FROM Users WHERE Kaard_ID = @kaartID", con);
  40.             cmd.Parameters.AddWithValue("@kaartID", _kaartID);
  41.             MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
  42.             DataTable dt = new DataTable();
  43.             adp.Fill(dt);
  44.             if(dt.Rows.Count != 1)
  45.             {
  46.                 //error
  47.                 return scannedUser = new User();
  48.             }
  49.             Console.WriteLine(dt.Rows[0]["Naam"].ToString());
  50.             scannedUser = new User(
  51.                     dt.Rows[0]["Naam"].ToString(),
  52.                     dt.Rows[0]["lastname"].ToString(),
  53.                     Convert.ToDateTime(dt.Rows[0]["leeftijd"]),
  54.                     Convert.ToDateTime(dt.Rows[0]["Kaart_Gemaakt"]),
  55.                     Convert.ToDateTime(dt.Rows[0]["Laatst_Gecheckt"]),
  56.                     Convert.ToInt32(dt.Rows[0]["id"]),
  57.                     Convert.ToDouble(dt.Rows[0]["Money"]),
  58.                     dt.Rows[0]["Kaard_ID"].ToString(),
  59.                     Convert.ToBoolean(dt.Rows[0]["Abonnement"]),
  60.                     Convert.ToBoolean(dt.Rows[0]["Geparkeerd"]));
  61.  
  62.             return scannedUser;
  63.  
  64.  
  65.  
  66.  
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement