Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
X/EnigmaX/Classes/DBConnect.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
150 lines (119 sloc)
4.69 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using MySql.Data.MySqlClient; | |
namespace EnigmaX | |
{ | |
public class DBConnect | |
{ | |
private MySqlConnection connection; | |
public DBConnect() | |
{ | |
Initialize(); | |
} | |
private void Initialize() | |
{ | |
connection = new MySqlConnection("Server=4156.hostgator.com; database=ttech_enigma; UID=ttech_enigma; password=JaQE7eKrY4Rr"); | |
} | |
private bool OpenConnection() | |
{ | |
try | |
{ | |
connection.Open(); | |
return true; | |
} | |
catch (MySqlException ex) | |
{ | |
//When handling errors, you can your application's response based | |
//on the error number. | |
//The two most common error numbers when connecting are as follows: | |
//0: Cannot connect to server. | |
//1045: Invalid user name and/or password. | |
switch (ex.Number) | |
{ | |
case 0: | |
//GlobalFunctions.showDialog("Database Error", "Couldn't connect to the Database.", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); | |
break; | |
case 1045: | |
// GlobalFunctions.showDialog("Database Error", "Database Credentials are Incorrect", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); | |
break; | |
} | |
return false; | |
} | |
} | |
private bool CloseConnection() | |
{ | |
try | |
{ | |
connection.Close(); | |
return true; | |
} | |
catch (MySqlException ex) | |
{ | |
// GlobalFunctions.showDialog("Database Error", ex.ToString(), System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); | |
return false; | |
} | |
} | |
public void WriteCommand(string str) | |
{ | |
if (this.OpenConnection() == true) | |
{ | |
//create command and assign the query and connection from the constructor | |
MySqlCommand cmd = new MySqlCommand(str, connection); | |
//Execute command | |
cmd.ExecuteNonQuery(); | |
//close connection | |
this.CloseConnection(); | |
} | |
} | |
public List<Dictionary<string, string>> ReadCommand(string str, params string[] collect) | |
{ | |
List<Dictionary<string, string>> total = new List<Dictionary<string, string>>(); | |
if (this.OpenConnection() == true) | |
{ | |
//Create Command | |
MySqlCommand cmd = new MySqlCommand(str, connection); | |
//Create a data reader and Execute the command | |
MySqlDataReader dataReader = cmd.ExecuteReader(); | |
//Read the data and store them in the list | |
while (dataReader.Read()) | |
{ | |
Dictionary<string, string> tmp = new Dictionary<string, string>(); | |
foreach (string t in collect) | |
{ | |
tmp.Add(t, dataReader[t] + ""); | |
} | |
total.Add(tmp); | |
} | |
//close Data Reader | |
dataReader.Close(); | |
//close Connection | |
this.CloseConnection(); | |
//return list to be displayed | |
return total; | |
} | |
else | |
{ | |
return total; | |
} | |
} | |
/*public string ReadSingleCommand(string str) { | |
if (this.OpenConnection() == true) | |
{ | |
//Create Mysql Command | |
MySqlCommand cmd = new MySqlCommand(str, connection); | |
//ExecuteScalar will return one value | |
Count = int.Parse(cmd.ExecuteScalar() + ""); | |
//close Connection | |
this.CloseConnection(); | |
return Count; | |
} | |
else | |
{ | |
return Count; | |
} | |
}*/ | |
} | |
} |