Пример #1
0
        /// <summary>
        /// 根据主键删除数据库记录
        /// </summary>
        /// <param name="field"></param>
        public void Delete(FieldValueDictionary field)
        {
            string fieldName = field.GetFieldName();

            db.ExecuteNonQuery("delete from " + tableName + " where " + fieldName +
                               "=@" + fieldName, db.GetAdapter().CreateParameter("@" + fieldName, field.Value));
        }
Пример #2
0
        public Entity Get(FieldValueDictionary field)
        {
            string fieldName = field.GetFieldName();
            string sql       = "SELECT TOP 1 * FROM " + tableName +
                               " WHERE " + fieldName + "=@" + fieldName;

            Entity          obj = null;
            ColumnAttribute attr;
            Type            type = typeof(Entity);
            Assembly        ass  = type.Assembly;


            db.ExecuteReader(new SqlQuery(sql, new object[, ]
            {
                { "@" + fieldName, field.Value }
            }), rd =>
            {
                object[] attrs; //columnAttributes
                while (rd.Read())
                {
                    obj = ass.CreateInstance(type.ToString(), true) as Entity;
                    foreach (PropertyInfo p in properties)
                    {
                        attrs = p.GetCustomAttributes(typeof(ColumnAttribute), true);
                        if (attrs.Length == 0)
                        {
                            continue;
                        }
                        attr = attrs[0] as ColumnAttribute;
                        p.SetValue(obj, Convert.ChangeType(rd[attr.Name ?? p.Name],
                                                           p.PropertyType), null);
                    }
                    break;
                }
            });

            return(obj);
        }