Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using MySql.Data.MySqlClient;
  7.  
  8. namespace Mysql
  9. {
  10.     class DB
  11.     {
  12.         private MySqlConnection connection;
  13.         private string server;
  14.         private string user;
  15.         private string port;
  16.         private string pass;
  17.         private string db;
  18.  
  19.         public DB()
  20.         {
  21.             Initialize();
  22.         }
  23.  
  24.         private void Initialize()
  25.         {
  26.             server = Program.form1.HOST_TXT.Text;
  27.             user   = Program.form1.USER_TXT.Text;
  28.             port   = "3306";
  29.             pass   = Program.form1.PASS_TXT.Text;
  30.             db     = Program.form1.DB_TEXT.Text;
  31.  
  32.             string connectionString = "SERVER=" + server + ";" + "DATABASE=" + db + ";" + "UID=" + user + ";" + "PASSWORD=" + pass + ";";
  33.             connection = new MySqlConnection(connectionString);
  34.         }
  35.  
  36.         private bool OpenConnection()
  37.         {
  38.             try
  39.             {
  40.                 connection.Open();
  41.                 return true;
  42.             }
  43.             catch (MySqlException ex)
  44.             {
  45.                 switch (ex.Number)
  46.                 {
  47.                     case 0:
  48.                         MessageBox.Show("Cannot connect to server.");
  49.                         break;
  50.                     case 1045:
  51.                         MessageBox.Show("Invalid username/password.");
  52.                         break;
  53.                 }
  54.                 return false;
  55.             }
  56.         }
  57.  
  58.         private bool CloseConnection()
  59.         {
  60.             try
  61.             {
  62.                 connection.Close();
  63.                 return true;
  64.             }
  65.             catch (MySqlException ex)
  66.             {
  67.                 MessageBox.Show(ex.Message);
  68.                 return false;
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement