Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1.  
  2.     class DBConnect
  3.     {
  4.  
  5.         private MySqlConnection connection;
  6.         private string server;
  7.         private string database;
  8.         private string uid;
  9.         private string password;
  10.  
  11.         //Constructor
  12.         public DBConnect()
  13.         {
  14.             Initialize();
  15.         }
  16.  
  17.         //Initialize values
  18.         private void Initialize()
  19.         {
  20.             server = "localhost:3306";
  21.             database = "cirkuit_db";
  22.             uid = "root";
  23.             password = "";
  24.             string connectionString;
  25.             connectionString = "SERVER=" + server + ";" + "DATABASE=" +
  26.             database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
  27.  
  28.             connection = new MySqlConnection(connectionString);
  29.         }
  30.  
  31.         //open connection to database
  32.         public int OpenConnection()
  33.         {
  34.             int resp = 0;       //0 si se pudo conectar
  35.             try
  36.             {
  37.                 connection.Open();
  38.                 resp = 1;
  39.  
  40.             }
  41.             catch (MySqlException ex)
  42.             {
  43.                 //When handling errors, you can your application's response based
  44.                 //on the error number.
  45.                 //The two most common error numbers when connecting are as follows:
  46.                 //0: Cannot connect to server.
  47.                 //1045: Invalid user name and/or password.
  48.                 switch (ex.Number)
  49.                 {
  50.                     case 0:                      
  51.                         resp = 2; //no se puede conectar al servidor. contacte al administrador
  52.                         break;
  53.  
  54.                     case 1045:
  55.                        
  56.                         resp = 1045; // 1045 usuario o password invalido.. por favor trate otravez
  57.                         break;
  58.                 }
  59.  
  60.             }
  61.             return resp;
  62.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement