using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DB_Server
{
    /// <summary>
    /// internal 本项目使用
    /// </summary>
    public class MYSQL_Helper 
    { 
        public static DataTable GetDatatable(string con, string sql, params MySqlParameter[] par)
        {
            try
            {
                using (MySqlConnection conn = new MySqlConnection(con))
                {
                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    using (MySqlDataAdapter adapter = new MySqlDataAdapter(sql, con))
                    {
                        if (par != null && par.Length > 0)
                        {
                            adapter.SelectCommand.Parameters.AddRange(par);
                        }
                        DataTable dt = new DataTable();
                        adapter.Fill(dt);
                        adapter.SelectCommand.Parameters.Clear();
                        return dt;
                    }
                }
            }
            catch (Exception ex)
            { 
                throw new Exception("mysql GetDatatable errorr  " + ex.Message); 
            }
        }

        internal static int ExecuteSql(string con, string sql)
        {
            using (MySqlConnection connection = new MySqlConnection(con))
            {
                using (MySqlCommand cmd = new MySqlCommand(sql, connection))
                {
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (MySqlException e)
                    {
                        connection.Close();
                        throw e;
                    }
                }
            }
        }
    }
}
