Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DbConnect
- {
- /// <summary>
- /// Preliminary setup
- /// </summary>
- public static MySqlConnection Connection;
- public static string Plot;
- private string _server;
- private string _database;
- private string _uid;
- private string _password;
- public static string ConnectionString = null;
- /// <summary>
- /// Initialise
- /// </summary>
- public void Initialize()
- {
- try
- {
- XDocument doc = XDocument.Load("DbConnect.xml");
- Dictionary<string, string> values =
- doc.XPathSelectElements("//Connection[@name='default']")
- .Single()
- .Elements("Attribute")
- .ToDictionary(el => (string) el.Attribute("name"), el => (string) el.Attribute("value"));
- _server = values["server"];
- _database = values["database"];
- _uid = values["uid"];
- _password = values["password"];
- Plot = values["plot"];
- ConnectionString = String.Format("SERVER={0};DATABASE={1};UID={2};PASSWORD={3}",_server,_database,_uid,_password);
- Connection = new MySqlConnection(ConnectionString);
- }
- catch (FileNotFoundException)
- {
- Environment.Exit(0);
- }
- }
- /// <summary>
- /// DbConnect constructor
- /// </summary>
- public DbConnect()
- {
- Initialize();
- }
- /// <summary>
- /// Open connection to database
- /// </summary>
- public bool OpenConnection()
- {
- try
- {
- Connection.Open();
- return true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- return false;
- }
- }
- /// <summary>
- /// Close connection to database
- /// </summary>
- public bool CloseConnection()
- {
- try
- {
- Connection.Close();
- return true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement