using Microsoft.Data.SqlClient;

public static class DbHelper
{
    public static int ExecuteNonQuery(string query, Dictionary<string, object> parameters)
    {
        using var connection = new SqlConnection(AppSettings.GetConnectionString("DefaultConnection"));
        connection.Open();

        using var command = new SqlCommand(query, connection);
        foreach (var param in parameters)
            command.Parameters.AddWithValue(param.Key, param.Value ?? DBNull.Value);

        return command.ExecuteNonQuery();
    }

    public static List<Dictionary<string, object>> ExecuteQuery(string query)
    {
        using var connection = new SqlConnection(AppSettings.GetConnectionString("DefaultConnection"));
        connection.Open();

        using var command = new SqlCommand(query, connection);
        using var reader = command.ExecuteReader();

        var result = new List<Dictionary<string, object>>();
        while (reader.Read())
        {
            var row = new Dictionary<string, object>();
            for (int i = 0; i < reader.FieldCount; i++)
                row[reader.GetName(i)] = reader.GetValue(i);
            result.Add(row);
        }

        return result;
    }
}
