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
{
   
    public class SQLSERVER_Helper
    {
        public static DataTable GetDatatable(string con, string sql, params SqlParameter[] par)
        {
            using (SqlConnection connection = new SqlConnection(con))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(sql, connection))
                {
                    if (par != null && par.Length > 0)
                    {
                        sda.SelectCommand.Parameters.AddRange(par);
                    }
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    sda.SelectCommand.Parameters.Clear();
                    return dt;
                }
            }
        }

        public static int ExecuteSql(string con,string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(con))
            {
                using (SqlCommand cmd = new SqlCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (SqlException e)
                    {
                        connection.Close();
                        throw e;
                    }
                }
            }
        }

    }
}
