using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace Neshina1703
{
    public static partial class DB
    {
        public static string ConnectionString =
            "Server=cfif31.ru;Database=ISPr25-21_NeshinaPV_1703;Uid=ISPr25-21_NeshinaPV;Password=ISPr25-21_NeshinaPV;SslMode=none;";

        public static MySqlConnection GetConnection()
        {
            return new MySqlConnection(ConnectionString);
        }

        public static DataTable ExecuteQuery(string query, MySqlParameter[] parameters = null)
        {
            using (var conn = GetConnection())
            {
                using (var cmd = new MySqlCommand(query, conn))
                {
                    if (parameters != null)
                        cmd.Parameters.AddRange(parameters);

                    var adapter = new MySqlDataAdapter(cmd);
                    var table = new DataTable();
                    adapter.Fill(table);
                    return table;
                }
            }
        }

        public static int ExecuteCommand(string query, MySqlParameter[] parameters = null)
        {
            using (var conn = GetConnection())
            {
                using (var cmd = new MySqlCommand(query, conn))
                {
                    if (parameters != null)
                        cmd.Parameters.AddRange(parameters);

                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        public static object ExecuteScalar(string query, MySqlParameter[] parameters = null)
        {
            using (var conn = GetConnection())
            {
                using (var cmd = new MySqlCommand(query, conn))
                {
                    if (parameters != null)
                        cmd.Parameters.AddRange(parameters);

                    conn.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }
    }
}