Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. public class DbConnect
  2.     {
  3.         /// <summary>
  4.         ///     Preliminary setup
  5.         /// </summary>
  6.         public static MySqlConnection Connection;
  7.         public static string Plot;
  8.         private string _server;
  9.         private string _database;
  10.         private string _uid;
  11.         private string _password;
  12.         public static string ConnectionString = null;
  13.  
  14.  
  15.  
  16.         /// <summary>
  17.         ///     Initialise
  18.         /// </summary>
  19.         public void Initialize()
  20.         {
  21.             try
  22.             {
  23.                 XDocument doc = XDocument.Load("DbConnect.xml");
  24.                 Dictionary<string, string> values =
  25.                     doc.XPathSelectElements("//Connection[@name='default']")
  26.                         .Single()
  27.                         .Elements("Attribute")
  28.                         .ToDictionary(el => (string) el.Attribute("name"), el => (string) el.Attribute("value"));
  29.                 _server = values["server"];
  30.                 _database = values["database"];
  31.                 _uid = values["uid"];
  32.                 _password = values["password"];
  33.                 Plot = values["plot"];
  34.                 ConnectionString = String.Format("SERVER={0};DATABASE={1};UID={2};PASSWORD={3}",_server,_database,_uid,_password);
  35.                 Connection = new MySqlConnection(ConnectionString);
  36.             }
  37.             catch (FileNotFoundException)
  38.             {
  39.                 Environment.Exit(0);
  40.             }
  41.         }
  42.  
  43.  
  44.  
  45.         /// <summary>
  46.         ///     DbConnect constructor
  47.         /// </summary>
  48.         public DbConnect()
  49.         {
  50.             Initialize();
  51.         }
  52.  
  53.  
  54.  
  55.         /// <summary>
  56.         ///     Open connection to database
  57.         /// </summary>
  58.         public bool OpenConnection()
  59.         {
  60.             try
  61.             {
  62.                 Connection.Open();
  63.                 return true;
  64.             }
  65.             catch (Exception ex)
  66.             {
  67.                 MessageBox.Show(ex.ToString());
  68.                 return false;
  69.             }
  70.         }
  71.  
  72.  
  73.  
  74.         /// <summary>
  75.         ///     Close connection to database
  76.         /// </summary>
  77.         public bool CloseConnection()
  78.         {
  79.             try
  80.             {
  81.                 Connection.Close();
  82.                 return true;
  83.             }
  84.             catch (Exception ex)
  85.             {
  86.                 MessageBox.Show(ex.Message);
  87.                 return false;
  88.             }
  89.         }
  90.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement