Advertisement
Guest User

Untitled

a guest
Apr 16th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using MySql.Data.MySqlClient;
  2. using System.Windows.Forms;
  3.  
  4. namespace pControll
  5. {
  6.     public class Database
  7.     {
  8.         private string mysqlServer   = pControll.Properties.Resources.mysqlServer;
  9.         private string mysqlDatabase = pControll.Properties.Resources.mysqlDatabase;
  10.         private string mysqlUsername = pControll.Properties.Resources.mysqlUsername;
  11.         private string mysqlPassword = pControll.Properties.Resources.mysqlPassword;
  12.  
  13.         private static Database mysqlInstance;
  14.         private MySqlConnection mysqlConnection;
  15.  
  16.         private static string errorMessage;
  17.  
  18.         private Database()
  19.         {
  20.             mysqlConnection = createConnection();
  21.             mysqlConnection.Open();
  22.         }
  23.  
  24.         public static Database getMysqlInstance()
  25.         {
  26.             if (mysqlInstance == null)
  27.                 mysqlInstance = new Database();
  28.  
  29.             return mysqlInstance;
  30.         }
  31.  
  32.         public MySqlConnection getMysqlConnection()
  33.         {
  34.             return mysqlConnection;
  35.         }
  36.  
  37.         private MySqlConnection createConnection()
  38.         {
  39.             MySqlConnection connection = new MySqlConnection();
  40.             string connectionString = "Server="+mysqlServer+"; database="+mysqlDatabase+"; UID="+mysqlUsername+"; password="+mysqlPassword;
  41.  
  42.             connection.ConnectionString = connectionString;
  43.  
  44.             return connection;
  45.         }
  46.  
  47.         public static string getErrorMessage(int errorCode)
  48.         {
  49.             switch (errorCode)
  50.             {
  51.                 case 0:
  52.                     errorMessage = "Sikertelen csatlakozás az adatbázishoz";
  53.                     break;
  54.                 case 1042:
  55.                     errorMessage = "Sikertelen csatlakozás az adatbázis szerverhez";
  56.                     break;
  57.                 default:
  58.                     errorMessage = null;
  59.                     break;
  60.             }
  61.  
  62.             return errorMessage;
  63.         }
  64.  
  65.         public static void showErorrMessage(string errorMessage)
  66.         {
  67.             if(errorMessage == null)
  68.                 errorMessage = "Nem definiált hibaüzenet";
  69.  
  70.             MessageBox.Show(errorMessage, "Adatbázis hiba", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  71.         }
  72.  
  73.         ~Database()
  74.         {
  75.             mysqlConnection.Close();
  76.         }
  77.     }
  78. }
  79.  
  80. using MySql.Data.MySqlClient;
  81. using System;
  82. using System.Windows.Forms;
  83.  
  84. namespace pControll
  85. {
  86.     public partial class Form2 : childForm
  87.     {
  88.  
  89.         public Form2()
  90.         {
  91.             InitializeComponent();
  92.         }
  93.  
  94.         private void button1_Click(object sender, EventArgs e)
  95.         {
  96.             try
  97.             {
  98.                 MySqlCommand mysqlQuery = new MySqlCommand("SELECT* FROM orders_semilab WHERE pSzam = 'P3560/1'", Database.getMysqlInstance().getMysqlConnection());
  99.                 MySqlDataReader mysqlReader = mysqlQuery.ExecuteReader();
  100.  
  101.                 if (mysqlReader.Read())
  102.                 {
  103.                     MessageBox.Show(mysqlReader["megnevezes"].ToString());
  104.                 }
  105.  
  106.                 mysqlReader.Close();
  107.             }
  108.             catch (MySqlException ex)
  109.             {
  110.                 Database.showErorrMessage(Database.getErrorMessage(ex.Number));
  111.             }
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement