Advertisement
Bubusuk

Untitled

Mar 9th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.67 KB | None | 0 0
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Data;
  4. using System.Windows.Forms;
  5.  
  6. namespace WindowsFormsApplication1
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         public static string user = "***";
  11.         public static string pass = "**";
  12.         public static string ip = "***";
  13.         public static string database = "***";
  14.         public static MySqlConnection conn = new MySqlConnection();
  15.  
  16.         /*
  17.         Připojování do databaze  
  18.         */
  19.         public static void startMYsql()
  20.         {
  21.             string data = "server=" + Form1.ip + ";uid=" + Form1.user + ";pwd=" + Form1.pass + ";database=" + Form1.database + ";";
  22.  
  23.             if (conn.State != ConnectionState.Open)
  24.             {
  25.                 conn.ConnectionString = data;
  26.                 conn.Open();
  27.             }
  28.         }
  29.         public Form1()
  30.         {
  31.             InitializeComponent();
  32.             startMYsql();
  33.         }
  34.  
  35.         private void Form1_Load(object sender, EventArgs e)
  36.         {
  37.             refreshList();
  38.         }
  39.         /*
  40.         Načítání prvků z databaze a vložení do listu
  41.         Udělano jako funkce zdůvodu volání v dalších částech programu
  42.        
  43.          
  44.         */
  45.         private void refreshList()
  46.         {
  47.             try
  48.             {
  49.                 listBox1.Items.Clear();
  50.                 MySqlDataReader cti = null;
  51.                 string dotaz = "SELECT * FROM country";
  52.                 MySqlCommand cmd = new MySqlCommand(dotaz, Form1.conn);
  53.                 cti = cmd.ExecuteReader();
  54.                 while (cti.Read())
  55.                 {
  56.                     listBox1.Items.Add(new Country(cti.GetInt16(0), cti.GetString(1)));
  57.                 }
  58.                 cti.Close();
  59.             }
  60.             catch
  61.             {
  62.                 MessageBox.Show("Někde nastala chyba");
  63.             }
  64.         }
  65.         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  66.         {
  67.             show_cities.Enabled = true;
  68.             try
  69.             {
  70.                 Country coun = (Country)listBox1.SelectedItem;
  71.                 textBox1.Text = coun.nazev;
  72.                 id.Text = coun.id.ToString();
  73.             }
  74.             catch
  75.             {
  76.  
  77.             }
  78.         }
  79.  
  80.         /*
  81.         Přidání prvku do databaze
  82.         */
  83.         private void add_country_Click(object sender, EventArgs e)
  84.         {
  85.             try
  86.             {
  87.                 MySqlCommand command = conn.CreateCommand();
  88.                 command.CommandText = "INSERT INTO country(nazev) VALUES(?nazev)";
  89.                 command.Parameters.AddWithValue("?nazev", textBox1.Text);
  90.                 command.ExecuteNonQuery();
  91.                 MessageBox.Show("Hotovo");
  92.                 refreshList();
  93.             }
  94.             catch
  95.             {
  96.                 MessageBox.Show("Někde nastala chyba");
  97.             }
  98.         }
  99.         /*
  100.         Aktualizace nazvu země  
  101.         */
  102.         private void edit_country_Click(object sender, EventArgs e)
  103.         {
  104.             try
  105.             {
  106.                 MySqlCommand command = conn.CreateCommand();
  107.                 command.CommandText = "UPDATE country SET nazev = ?nazev WHERE PK_idc = ?id";
  108.                 command.Parameters.AddWithValue("?nazev", textBox1.Text);
  109.                 command.Parameters.AddWithValue("?id", id.Text);
  110.                 command.ExecuteNonQuery();
  111.                 MessageBox.Show("Hotovo");
  112.                 refreshList();
  113.             }
  114.             catch
  115.             {
  116.                 MessageBox.Show("Někde nastala chyba");
  117.             }
  118.         }
  119.  
  120.         private void delete_country_Click(object sender, EventArgs e)
  121.         {
  122.             try
  123.             {
  124.                 MySqlCommand command = conn.CreateCommand();
  125.                 command.CommandText = "DELETE FROM country WHERE PK_idc = ?id";
  126.                 command.Parameters.AddWithValue("?id", id.Text);
  127.                 command.ExecuteNonQuery();
  128.                 MessageBox.Show("Hotovo");
  129.                 refreshList();
  130.             }
  131.             catch
  132.             {
  133.                 MessageBox.Show("Někde nastala chyba");
  134.             }
  135.         }
  136.  
  137.         private void show_cities_Click(object sender, EventArgs e)
  138.         {
  139.             Country coun = (Country)listBox1.SelectedItem;
  140.             new Form2(coun.id).Show();
  141.         }
  142.     }
  143.     class Country
  144.     {
  145.         public int id;
  146.         public string nazev;
  147.         public Country(int id, string nazev)
  148.         {
  149.             this.id =  id;
  150.             this.nazev = nazev;
  151.         }
  152.         public override string ToString()
  153.         {
  154.             return this.nazev;
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement