Esempio n. 1
0
        public T Convert(IDataReader reader)
        {
            var    item       = default(T);
            object boxed      = item;
            var    columns    = new GetColumns().From(reader);
            var    properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);

            while (reader.Read())
            {
                foreach (var property in properties)
                {
                    if (columns.Contains(property.Name))
                    {
                        object value = reader[property.Name];
                        // if dbnull change to c# null
                        if (value == DBNull.Value)
                        {
                            value = null;
                        }
                        property.SetValue(boxed, value);
                    }
                }
                item = (T)boxed;
                break;
            }
            return(item);
        }
Esempio n. 2
0
        public List <T> Convert(IDataReader reader)
        {
            var list       = new List <T>();
            var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var columns    = new GetColumns().From(reader);

            while (reader.Read())
            {
                var item = System.Activator.CreateInstance <T>();
                foreach (var property in properties)
                {
                    if (columns.Contains(property.Name))
                    {
                        property.SetValue(item, reader[property.Name]);
                    }
                }
                list.Add(item);
            }
            return(list);
        }
Esempio n. 3
0
        public List <T> Convert(IDataReader reader)
        {
            var list       = new List <T>();
            var properties = typeof(T).GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            var columns    = new GetColumns().From(reader);

            while (reader.Read())
            {
                var    item = default(T);
                object obj  = item; // box
                foreach (var property in properties)
                {
                    if (columns.Contains(property.Name))
                    {
                        property.SetValue(obj, reader[property.Name]);
                    }
                }
                item = (T)obj; // unbox
                list.Add(item);
            }
            return(list);
        }
Esempio n. 4
0
        public string BuildUpdateSql()
        {
            var tableName    = this.tableName.Get(instance);
            var properties   = typeof(T).GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            var tableColumns = new GetColumns().From(tableName, db, cache);
            var setters      = "";
            var id           = "0";

            foreach (var property in properties)
            {
                if (property.Name.ToUpper() == "ID")
                {
                    var objId = property.GetValue(instance);
                    id = objId == null ? "0" : objId.ToString();
                    //id = property.GetValue(instance).ToStringOr("0");
                    continue;
                }
                if (!tableColumns.Contains(property.Name))
                {
                    continue;
                }

                var value = property.GetValue(instance);
                if (value is null)
                {
                    value = DBNull.Value;
                }

                setters += $"{property.Name} = @{property.Name}, ";

                command.Parameters.AddWithValue("@" + property.Name, value);
            }
            setters = setters.Substring(0, setters.Length - 2);
            var sql = $@"update [{tableName}] set {setters} where id = {id}";

            return(sql);
        }
Esempio n. 5
0
        public string BuildInsertSql()
        {
            var tableName    = this.tableName.Get(instance);
            var properties   = typeof(T).GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            var tableColumns = new GetColumns().From(tableName, db, cache);
            var columnNames  = "";
            var values       = "";

            foreach (var property in properties)
            {
                if (property.Name.ToUpper() == "ID")
                {
                    continue;
                }
                if (!tableColumns.Contains(property.Name))
                {
                    continue;
                }

                var value = property.GetValue(instance);
                if (value is null)
                {
                    value = DBNull.Value;
                }
                columnNames += property.Name + ", ";

                command.Parameters.AddWithValue("@" + property.Name, value);
                values += "@" + property.Name + ", ";
            }
            columnNames = columnNames.Substring(0, columnNames.Length - 2);
            values      = values.Substring(0, values.Length - 2);
            var sql = $@"INSERT INTO [{tableName}] ({columnNames}) VALUES ({values})
            SELECT @@IDENTITY Id";

            return(sql);
        }