Untitled

 avatar
unknown
plain_text
a year ago
3.3 kB
8
Indexable
    public class ClassBooking
    {
        // Database Settings
        private const string DB_HOST = "localhost";
        private const string DB_NAME = "mybooking";   //chance this to your database name
        private const string DB_USERNAME = "root";
        private const string DB_PASSWORD = "";

        public string ConnString = $"SERVER={DB_HOST};DATABASE={DB_NAME};UID={DB_USERNAME};PASSWORD={DB_PASSWORD};";

        public MySqlConnection DBConnection;

        public ClassBooking()
        {
            DBConnection = new MySqlConnection(ConnString);
        }

        public bool Connect()
        {
            try
            {
                if (DBConnection.State != ConnectionState.Open)
                {
                    DBConnection.Open();
                }
                return true; // Connection successful
            }
            catch (Exception ex)
            {
                Debug.Print($"ERROR: {ex.Message}");
                return false; // Connection failed
            }
        }

        public void Disconnect()
        {
            if (DBConnection.State == ConnectionState.Open)
            {
                DBConnection.Close();
            }
        }


        public DataTable GetRows(string sql, params MySqlParameter[] parameters)
        {
            DataTable dt = new DataTable();
            try
            {
                if (Connect()) // Ensure connection is open
                {
                    using (MySqlCommand cmd = new MySqlCommand(sql, DBConnection))
                    {
                        cmd.Parameters.AddRange(parameters); // Add parameters to prevent SQL injection

                        using (MySqlDataReader reader = cmd.ExecuteReader())
                        {
                            dt.Load(reader);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // Log the error for better debugging
                MessageBox.Show($"Error executing query: {ex.Message}");

            }
            finally
            {
                Disconnect(); // Always close connection, even in case of exceptions
            }
            return dt;
        }

        public bool ExecuteQuery(string SQLQuery)
        {
            if (Connect())
            {
                using (MySqlCommand cmd = new MySqlCommand(SQLQuery, DBConnection))
                {
                    try
                    {
                        cmd.ExecuteNonQuery();
                        Disconnect();
                        return true;
                    }
                    catch (MySqlException ex)
                    {
                        MessageBox.Show($"Database Error: {ex.Message}");
                        Debug.Print($"Database Error: {ex.Message}");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"Error: {ex.Message}");
                        Debug.Print($"Error: {ex.Message}");
                    }
                }
            }
            return false;
        }
    }
}
Editor is loading...
Leave a Comment