viernes, 5 de junio de 2015

C# - Reemplazar caracteres especiales

/*Arreglo con caracteres*/
 private string[] caracteres =
    {
        "Á", "Á",
        "À", "À",
        "Â", "Â",
        "Ä", "Ä",
        "Ã", "Ã",
        "Å", "Å",
        "á", "á",
        "à", "à",
        "â", "â",
        "ä", "ä",
        "ã", "ã",
        "å", "å",
        "É", "É",
        "È", "È",
        "Ê", "Ê",
        "Ë", "Ë",
        "é", "é",
        "è", "è",
        "ê", "ê",
        "ë", "ë",
        "Í", "Í",
        "Ì", "Ì",
        "Î", "Î",
        "Ï", "Ï",
        "í", "í",
        "ì", "ì",
        "î", "î",
        "ï", "ï",
        "Ó", "Ó",
        "Ò", "Ò",
        "Ô", "Ô",
        "Ö", "Ö",
        "Õ", "Õ",
        "ó", "ó",
        "ò", "ò",
        "ô", "ô",
        "ö", "ö",
        "õ", "õ",
        "Ú", "Ú",
        "Ù", "Ù",
        "Û", "Û",
        "Ü", "Ü",
        "ú", "ú",
        "ù", "ù",
        "û", "û",
        "ü", "ü",
        "Ý", "Ý",
        "ý", "ý",
        "ÿ", "ÿ",
        "ñ", "ñ",
        "Ñ", "Ñ",
        "Ç", "Ç",
        "ç", "ç",
        "¡", "¡",
        "¿", "¿",
        "´", "´",
        "·", "·",
        "¸", "¸",
        "«", "«",
        "»", "»",
        "¨", "¨",
        "Æ", "Æ",
        "æ", "æ",
        "ß", "ß",
        "µ", "µ",
        "Ð", "Ð",
        "ð", "ð",
        "Þ", "Þ",
        "þ", "þ",
        "¢", "¢",
        "£", "£",
        "¤", "¤",
        "¥", "¥",
        "€", "€",
        "$", "$",
        "¹", "¹",
        "²", "²",
        "³", "³",
        "×", "×",
        "÷", "÷",
        "±", "±",
        "¼", "¼",
        "½", "½",
        "¾", "¾",
        "Ø", "Ø",
        "ø", "ø",
        "¬", "¬",
        "<", "&lt;",
        ">", "&gt;",
        "&", "&amp;",
        " ", "&nbsp;",
        "\"", "&quot;",
        "º", "&ordm;",
        "ª", "&ordf;",
        "©", "&copy;",
        "®", "&reg;",
        "°", "&deg;",
        "¦", "&brvbar;",
        "§", "&sect;",
        "¶", "&para;",
        "¯", "&macr;"
    };

        /// <summary>
        /// ReemplazarCaracteresEspeciales
        /// </summary>
        /// <param name="origen">string con caracteres a limpiar</param>
        /// <returns>string limpio</returns>
        public string ReemplazarCaracteresEspeciales(string origen)
        {
            string destino = "";
            List<string> listCaracteres = new List<string>();
            for (int i = 0; i < origen.Length; i++)
            {
                listCaracteres.Add(origen[i].ToString());
            }

            for (int i = 0; i < listCaracteres.Count; i++)
            {
                for (int j = 0; j < caracteres.Length; j = j + 2)
                {
                    if (listCaracteres[i] == caracteres[j])
                    {
                        listCaracteres[i] = listCaracteres[i].Replace(listCaracteres[i], caracteres[j + 1]);
                        j = caracteres.Length + 1;
                    }
                }
            }

            for (int i = 0; i < listCaracteres.Count; i++)
            {
                destino = destino + listCaracteres[i];
            }

            return destino;
        }

SQL - Agregar ID en consulta ROW_NUMBER

SELECT  ROW_NUMBER() OVER(ORDER BY campo_1 ASC) AS ID
        ,campo_1
        ,campo_2
        ,campo_3
FROM    mi_tabla

SQL - Cantidad de registros por Tabla en BD

USE [mi_base_de_datos]
/*Cantidad de registros del Modelo*/
select substring(obj.name, 1, 50) as Tabla, ind.rows as Registros
from sysobjects as obj
inner join sysindexes as ind on obj.id = ind.id
where obj.xtype = 'u'
and ind.indid < 2
order by ind.rows desc

SQL - Deshabilitar Triggers o Trigger de una tabla

/*deshabilitar */
ALTER TABLE mi_tabla DISABLE TRIGGER ALL
o
ALTER TABLE mi_tabla DISABLE TRIGGER nombre_trigger


/*habilitar */
ALTER TABLE mi_tabla ENABLE TRIGGER ALL
o
ALTER TABLE mi_tabla ENABLE TRIGGER nombre_trigger

C# - Convertir lista en DataTable

/// <summary>
    ///Convierte Lista en DataTable
    /// </summary>
    /// <param name="items">Lista a convertir de tipo object</param>
    /// <param name="nameService">Nombre a dar al DataTable</param>
    /// <returns></returns>
    public static DataTable ConvertirListaToDataTable(List<object> items, string nameService)
    {
        // Instancia del objeto a devolver
        var returnValue = new DataTable();
        // Información del tipo de datos de los elementos del List
        Type itemsType = items.First().GetType();
        /*Se Recorren las propiedades para crear las columnas del datatable y
        pregunta si el objeto tiene estrcutra.Si tiene entra.*/
        if (itemsType.GetProperties().Any())
        {
            foreach (PropertyInfo prop in itemsType.GetProperties())
            {
                //Se Crea y agrega una columna por cada propiedad de la entidad
                var column = new DataColumn(prop.Name);

                var propType = Nullable.GetUnderlyingType(prop.PropertyType)
                  ?? prop.PropertyType;

                column.DataType = propType;
                returnValue.Columns.Add(column);
            }
            int j;
            /* Se recorre la colección para guardar los datos
             en el DataTable*/
            foreach (var item in items)
            {
                j = 0;
                object[] newRow = new object[returnValue.Columns.Count];
                /* Se vuelve a recorrer las propiedades de cada item para
                 obtener su valor y guardarlo en la fila de la tabla*/
                foreach (PropertyInfo prop in itemsType.GetProperties())
                {
                    newRow[j] = prop.GetValue(item, null);
                    j++;
                }
                returnValue.Rows.Add(newRow);
            }
        }
        //si el objeto no tiene estructura, se arma una.
        else
        {
            var newRow = new object[items.Count];
            for (int i = 0; i < items.Count; i++)
            {
                var colName = nameService + i;
                var column = new DataColumn(colName);
                column.DataType = typeof(string);
                returnValue.Columns.Add(column);
            }
            for (int i = 0; i < items.Count; i++)
            {
                newRow[i] = items[i].ToString();
            }
            returnValue.Rows.Add(newRow);
        }
        // devolución del DataTable
        return returnValue;
    }

miércoles, 3 de junio de 2015

SQL - Editar tipo de dato en clave compuesta

Generar DROP AND CREATE de la tabla a modificar.



De esa forma se obtienen las CONSTRAINT que se deben eliminar  para modificar tipo de dato y luego agregar.
Ejemplo de modificacion de Tipo de dato en tabla con clave compuesta:

ALTER TABLE t_documentos
DROP CONSTRAINT DF_t_documentos_fecha_creacion,DF_t_documentos_estado
GO
ALTER TABLE t_documentos DROP CONSTRAINT PK_t_documentos
GO
ALTER TABLE t_documentos ALTER COLUMN correlativo INT NOT NULL
GO
ALTER TABLE t_documentos
ADD CONSTRAINT [PK_t_documentos] PRIMARY KEY (numero, tipo_doc, correlativo)
GO
ALTER TABLE [dbo].[t_documentos] ADD  CONSTRAINT [DF_t_documentos_fecha_creacion]  DEFAULT (getdate()) FOR [fecha_creacion]
GO
ALTER TABLE [dbo].[t_documentos] ADD  CONSTRAINT [DF_t_documentos_estado]  DEFAULT ((0)) FOR [estado]
GO