Ejemplo n.º 1
0
        /// <summary>
        /// Executes commandText as SQL and invokes the ReaderFunc delegate for each record
        /// </summary>
        /// <param name="commandText">The SQL to be executed</param>
        /// <param name="func">The delegate that runs foreach record</param>
        /// <param name="variables">The SQL variables</param>
        /// <returns>true if SqlDataReader has any rows else false</returns>
        public bool ExecuteReader(string commandText, ReaderFunc func, dynamic variables)
        {
            bool hasRows = false;

            using (var con = NewConnection()) {
                con.Open();
                using (var cmd = con.CreateCommand()) {
                    // Setup command:
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.CommandText = commandText;

                    if (variables != null)
                    {
                        System.Reflection.PropertyInfo[] varprops = variables.GetType().GetProperties();
                        foreach (var prop in varprops)
                        {
                            cmd.Parameters.AddWithValue(prop.Name, prop.GetValue(variables));
                        }
                    }

                    // Start reading:
                    using (var reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            func(reader);
                        }

                        hasRows = reader.HasRows;
                        reader.Close();
                    }
                }
                con.Close();
            }
            return(hasRows);
        }
Ejemplo n.º 2
0
        public virtual void ReadValue(BinaryReader binaryReader, ref TClass instance)
        {
            var value = ReaderFunc?.Invoke(binaryReader, instance) ?? ReadObject(binaryReader);

            if (DefaultValue is Enum)
            {
                value = Enum.Parse(typeof(TValue), value.ToString()); //this way enum can be read as byte, short, int, long, and string
            }
            _property.SetValue(instance, value);
        }
Ejemplo n.º 3
0
 internal Reader(ReaderFunc <R, A> func)
 {
     Func = func;
 }