Skip to content
Permalink
6464816278
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
150 lines (119 sloc) 4.69 KB
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;
}
}*/
}
}