Advertisement
Guest User

database

a guest
Feb 14th, 2019
2,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.64 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;
  7. using MySql.Data.MySqlClient;
  8. using System.IO;
  9. using System.Drawing;
  10.  
  11.  
  12. namespace szakdolgozat_v._0._1
  13. {
  14.     class database
  15.     {
  16.         string kapcs_string = @"datasource=localhost;database=dbp_p14_vbm;username=root;password=;charset=utf8";
  17.         public MySqlConnection kapcs_mysql;
  18.         MySqlDataAdapter adapter_mysql = new MySqlDataAdapter();
  19.  
  20.         public bool megnyitas()
  21.         {
  22.             try { kapcs_mysql = new MySqlConnection(kapcs_string); kapcs_mysql.Open(); return true; }
  23.             catch { return false; }
  24.         }
  25.         public bool bezaras()
  26.         {
  27.             try { kapcs_mysql.Close(); return true; }
  28.             catch { return false; }
  29.         }
  30.  
  31.         public bool vegrehajt(string LKS)
  32.         {
  33.             bezaras();
  34.             if (megnyitas())
  35.             {
  36.                 MySqlCommand lekerdezes = new MySqlCommand(LKS, kapcs_mysql);
  37.                 lekerdezes.ExecuteNonQuery();
  38.                 return true;
  39.             }
  40.             return false;
  41.         }
  42.         public string erteket_ad(string LKS)
  43.         {
  44.             bezaras();
  45.             if (megnyitas())
  46.             {
  47.                 MySqlCommand lekerdezes = new MySqlCommand(LKS, kapcs_mysql);
  48.                 try
  49.                 {
  50.                     return lekerdezes.ExecuteScalar().ToString();
  51.                 }
  52.                 catch
  53.                 {
  54.                     return "hiba1";
  55.                 }
  56.             }
  57.             return "hiba";
  58.         }
  59.         public string[] erteket_ad_rekord(string LKS)
  60.         {
  61.             bezaras();
  62.             if (megnyitas())
  63.             {
  64.                 MySqlCommand lekerdezes = new MySqlCommand(LKS, kapcs_mysql);
  65.                 try
  66.                 {
  67.                     MySqlDataReader olvaso;
  68.                     olvaso = lekerdezes.ExecuteReader();
  69.                     int n = olvaso.FieldCount;
  70.                     if (n > 0)
  71.                     {
  72.                         string[] sv = new string[n];
  73.                         olvaso.Read();
  74.                         for (int i = 0; i < n; i++)
  75.                         {
  76.                             sv[i] = olvaso[i].ToString();
  77.                         }
  78.                         return sv;
  79.                     }
  80.                     else
  81.                     {
  82.                         return null;
  83.                     }
  84.                 }
  85.                 catch
  86.                 {
  87.                     return null;
  88.                 }
  89.             }
  90.             return null;
  91.         }
  92.         public DataSet tablazatot_ad(string LKS)
  93.         {
  94.             bezaras();
  95.             DataSet adatok = new DataSet();
  96.             if (megnyitas())
  97.             {
  98.                 adapter_mysql.SelectCommand = new MySqlCommand(LKS, kapcs_mysql);
  99.                 adapter_mysql.Fill(adatok);
  100.             }
  101.             return adatok;
  102.         }
  103.        
  104.         public void keppel_feltolt(string imgPath, string LKS)
  105.         {
  106.             bezaras();
  107.             if (megnyitas())
  108.             {
  109.                 FileStream fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
  110.                 BufferedStream bf = new BufferedStream(fs);
  111.                 byte[] buffer = new byte[bf.Length];
  112.                 bf.Read(buffer, 0, buffer.Length);
  113.                 byte[] buffer_new = buffer;
  114.  
  115.                 MySqlCommand command = new MySqlCommand("", kapcs_mysql);
  116.                 LKS += " @image)";
  117.                 command.CommandText = LKS;
  118.                 command.Parameters.AddWithValue("@image", buffer_new);
  119.                 command.ExecuteNonQuery();
  120.             }
  121.         }
  122.  
  123.         public void keppel_frissit(string hol, string mi, string imgPath, string LKS )
  124.         {
  125.             bezaras();
  126.             if (megnyitas())
  127.             {
  128.                 FileStream fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
  129.                 BufferedStream bf = new BufferedStream(fs);
  130.                 byte[] buffer = new byte[bf.Length];
  131.                 bf.Read(buffer, 0, buffer.Length);
  132.                 byte[] buffer_new = buffer;
  133.  
  134.                 MySqlCommand command = new MySqlCommand("", kapcs_mysql);
  135.                 LKS += "@image WHERE "+ hol+"='"+mi+"'";
  136.                 command.CommandText = LKS;
  137.                 command.Parameters.AddWithValue("@image", buffer_new);
  138.                 command.ExecuteNonQuery();
  139.             }
  140.         }
  141.         public Image kepet_ad(string LKS)
  142.         {
  143.             bezaras();
  144.             DataSet adatok = new DataSet();
  145.             Image image = null;
  146.             if (megnyitas())
  147.             {
  148.                 adapter_mysql.SelectCommand = new MySqlCommand(LKS, kapcs_mysql);
  149.                 adapter_mysql.Fill(adatok,"image");
  150.  
  151.                 Byte[] imageBytes = (Byte[])(adatok.Tables["image"].Rows[0]["picture"]);
  152.                 MemoryStream buf = new MemoryStream(imageBytes);                  
  153.                 image = Image.FromStream(buf);
  154.             }
  155.             return image;
  156.         }
  157.  
  158.         public List<string> listat_ad(string LKS)
  159.         {
  160.             bezaras();
  161.             List<string> ertekek = new List<string>();
  162.             if (megnyitas())
  163.             {
  164.                 MySqlCommand lekerdezes = new MySqlCommand(LKS, kapcs_mysql);
  165.                 MySqlDataReader olvaso = lekerdezes.ExecuteReader();
  166.                 while (olvaso.Read())
  167.                 {
  168.                     ertekek.Add(olvaso[0].ToString());
  169.                 }
  170.             }
  171.             return ertekek;
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement