Grid Fill dynamically with dapper framework return

3

I have a screen written in VB.NET WinForms, where the goal of it will be to execute SQL commands without the need to open an sql studio.

For simpler commands like delete, update, alter table among others, it already works perfectly. Now, the idea is to create a gridview dynamically when the command is a select. Basically what Management Studio itself does when executing a SELECT statement, creating the columns and populating with the returned data.

I'm using Dapper Framework to run the commands, and the return of it is a "DapperRow" that I can not even get through.

I would like to know a way to create this kind of functionality, that is, execute a SELECT, get the return, and from this return I populate my gridview dynamically with the data returned from SELECT.

Thank you in advance.

    
asked by anonymous 03.02.2017 / 20:37

1 answer

2

DapperRow can be treated as a dynamic object. I got this idea from an old code of mine. Help, but I admit I can do better. I think that's a bit slow.

public statis class DapperExtensions
{
    /// <summary>
    /// Transforma uma lista de DapperRows em uma lista tipada. 
    /// </summary>
    /// <typeparam name="T">Uma classe que seja um Model.</typeparam>
    /// <param name="list">A lista de DapperRows.</param>
    /// <returns></returns>
    public static IEnumerable<T> ToTypedList<T>(this IEnumerable<dynamic> list)
        where T: class, new()
    {
        var properties = typeof(T).GetProperties();

        foreach (var element in list)
        {
            var obj = new T();
            foreach (var keyValue in ((IDictionary<string, object>) element).Where(e => e.Value != null) 
            {
                PropertyInfo property = properties.FirstOrDefault(p => p.Name == keyValue.Key);
                }

                if (property == null) continue;

                switch (property.PropertyType.ToString())
                {
                    case "System.Int16":
                        if (!String.IsNullOrEmpty(keyValue.Value.ToString()))
                        {
                            property.SetValue(obj, Convert.ToInt16(keyValue.Value));
                        }

                        break;
                    case "System.Int32":
                        if (!String.IsNullOrEmpty(keyValue.Value.ToString()))
                        {
                            property.SetValue(obj, Convert.ToInt32(keyValue.Value));
                        }

                        break;
                    case "System.Int64":
                        property.SetValue(obj, Convert.ToInt64(keyValue.Value));

                        break;
                    case "System.DateTime":
                        property.SetValue(obj, Convert.ToDateTime(keyValue.Value));

                        break;
                    case "System.Decimal":
                        property.SetValue(obj, Convert.ToDecimal(keyValue.Value));

                        break;
                    default:
                        if (keyValue.Value != null)
                        {
                            property.SetValue(obj, keyValue.Value);
                        }

                        break;
                }
            }

            yield return obj;
        }
    }
}

Usage:

var listaTipada = listaDeDapperRows.ToTypedList<MeuModel>();
    
03.02.2017 / 22:12