jueves, 14 de mayo de 2020

Using delegates. Basic Example


MULTICAST


ENCAPSULATION






class Program
    {
        static void Main(string[] args)
        {
            BookDb books = new BookDb();
            AddBooks(books);            
            Console.WriteLine("Soft cover book titles:");
            books.SearchSoftCoverBooks(PrintTitle);

            Total sum = new Total();
            books.SearchSoftCoverBooks(sum.CalculateTotalPrice);
            Console.WriteLine("Cumulative total price: {0}", sum.total.ToString());

            Console.ReadKey();
        }

        static void PrintTitle(Book book)
        {
            Console.WriteLine(" > {0}", book.title);
        }
        static void AddBooks(BookDb db)
        {
            db.AddBook("C programming", "Brian W.", 19.95m, true);
            db.AddBook("Unix 2.0", "William G.", 25.50m, true);
            db.AddBook("MS-DOS", "Smith E.", 12.75m, false);
            db.AddBook("Cleam code", "Nicholson A.", 22.50m, true);
        }
    }

    public class BookDb
    {
        ArrayList db = new ArrayList();

        public void AddBook(string title, string owner, decimal price, bool softCover)
        {
            db.Add(new Book(title, owner, price, softCover));
        }

        public void SearchSoftCoverBooks(SearchBooks searchBooks)
        {
            foreach(Book b in db)
            {
                if(b.softCover)
                {
                    searchBooks(b);
                }
            }
        }
    }

    public class Total
    {
        public decimal total;
        public void CalculateTotalPrice(Book book)
        {
            total += book.price;
        }
    }

    public delegate void SearchBooks(Book book);

    public class Book
    {
        public string title;
        public string owner;
        public decimal price;
        public bool softCover;

        public Book(string title, string owner, decimal price, bool softCover)
        {
            this.title = title;
            this.owner = owner;
            this.price = price;
            this.softCover = softCover;
        }
    }

Using Interfaces, Basic Examples

EJEMPLO 1


EJEMPLO 2





Installing your own Windows Service



PROGRAM.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;  //ADD THIS PACKAGE

namespace MyService
{
    class Program
    {
        static void Main(string[] args)
        {
            var host = HostFactory.Run(front =>
            {
                front.RunAsLocalSystem();
                front.SetServiceName("FileUpload");
                front.SetDisplayName("File Update Service");
                front.SetDescription("Servicio local de File Update Service");

                front.Service<FileUpdate>(back => 
                {
                    back.ConstructUsing(service => new FileUpdate());
                    back.WhenStarted(service => service.Start());
                    back.WhenStopped(service => service.Stop());
                });
            });

            int outvalue = (int)Convert.ChangeType(host, host.GetTypeCode());

            Environment.ExitCode = outvalue;
        }
    }
}

/*INSTALL SERVICE
*
*   COPY CODE TO SOME FOLDER. EXAMPLE: C:\TMP\CODE\
*   RUN WINDOWS SHELL CONSOLE AS ADMINISTRATOR
*   C:> CD C:\TMP\CODE\
*   C:\TMP\CODE\> MYSERVICE.EXE INSTALL START
* UNINSTALL SERVICE
*   RUN WINDOWS SHELL CONSOLE AS ADMINISTRATOR
*   C:> CD C:\TMP\CODE\
*   C:\TMP\CODE\> MYSERVICE.EXE UNINSTALL 
*  
*/

====================================================
CLASS FILEUPLOAD

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace MyService
{
    class FileUpdate
    {
        private readonly Timer time;

        public FileUpdate()
        {
            time = new Timer(1000)
            {
                AutoReset = true
            };
            time.Elapsed += Time_Elapsed;
        }

        private void Time_Elapsed(object sender, ElapsedEventArgs e)
        {
            string line = string.Format("{0}\n", DateTime.Now.ToString());
            File.AppendAllText(@"C:\tmp\timer.log", line);
        }

        public void Start()
        {
            time.Start();
        }

        public void Stop()
        {
            time.Stop();
        }
    }
}

miércoles, 29 de agosto de 2018

Publishing SSRS

Locally deploy you can Run this script as DBAFire.bat file:

@echo off
::Script Variables
SET LOGFILE="C:\DeploySSRS\RSScripterlog.txt"
SET REPORTSERVER=http://10.153.3.199/ReportServer
SET RS="RS.EXE"
SET TIMEOUT=60
::Clear Log file
IF EXIST %logfile% DEL %logfile%
::Run Scripts
 %rs% -i "C:\DeploySSRS\Commonscript.rss" -s "%REPORTSERVER%" -e Mgmt2010
ECHO Finished Load at TE% %TIME% >>%LOGFILE%
ECHO. >>%LOGFILE%
PAUSE

martes, 29 de mayo de 2018

SQL Comma separated string to rows and columns

DECLARE @Data NVARCHAR(MAX) = 'Jessica Espinosa, Gerente de Nomina, REG. PAT. L0165468104, R.F.C. WKA160429DD8'
DECLARE @Delimiter NVARCHAR(5) = ','
DECLARE @Table TABLE ( Data NVARCHAR(MAX), SequentialOrder INT IDENTITY(1, 1))

DECLARE @TextXml XML;
SELECT @TextXml = CAST('<d>' + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@Data, '&', '&amp;'), '<', '&lt;'), '>', '&gt;'), '"', '&quot;'), '''', '&apos;'), @Delimiter, '</d><d>') + '</d>' AS XML);

INSERT INTO @Table (Data)
SELECT Data = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(LTRIM(T.split.value('.', 'nvarchar(max)'))), '&amp;', '&'), '&lt;', '<'), '&gt;', '>'), '&quot;', '"'), '&apos;', '''')
FROM @TextXml.nodes('/d') T(Split)

SELECT
(SELECT Data FROM @Table WHERE SequentialOrder=1) [Signature1],
(SELECT Data FROM @Table WHERE SequentialOrder=2) [Signature2],
(SELECT Data FROM @Table WHERE SequentialOrder=3) [Signature3],
(SELECT Data FROM @Table WHERE SequentialOrder=4) [Signature4]

SELECT * FROM @Table

miércoles, 22 de febrero de 2017

SSIS Running ETL from stored procedured

CREATE Procedure [ETL].[uspExecute_SSIS_Package_ActivosLiquidos]

  @Anio nvarchar(4),

  @Mes nvarchar(2),

  @Ruta varchar(250),

  @ETLID BIGINT OUTPUT

AS
  SET NOCOUNT ON;

  DECLARE

     @AnioParam sql_variant = cast(@Anio as sql_variant),

     @MesParam sql_variant = cast(@Mes as sql_variant),

     @RutaParam sql_variant = cast(@Ruta as sql_variant),

     @IdReporteParam sql_variant = 'D3A6EED2-D3C7-4212-905B-963FC5AA8E82';

  PRINT 'Parámetros convertidos a sql variant'
    --Paso 1: Configurar datos del ETL
  DECLARE @RutaFolder nvarchar(50) = N'SHF',

      @RutaProyecto nvarchar(50) = N'INSTRUMENTOS FINANCIEROS',

      @NombrePaquete varchar(50) = N'SSIS_ActivosLiquidos.dtsx';



  EXEC [SSISDB].[catalog].[create_execution] @package_name=@NombrePaquete,

                 @execution_id=@ETLID OUTPUT,

                 @folder_name=@RutaFolder,

                 @project_name=@RutaProyecto,

                 @use32bitruntime=FALSE;

  PRINT 'Paso 2: Configurar parámetros para el ETL Año'

  EXEC [SSISDB].[catalog].[set_execution_parameter_value] @ETLID, 30, N'Anio', @AnioParam;

  PRINT 'Paso 2: Configurar parámetros para el ETL Mes'

  EXEC [SSISDB].[catalog].[set_execution_parameter_value] @ETLID, 30, N'Mes', @MesParam;

  PRINT 'Paso 2: Configurar parámetros para el ETL Ruta'
  EXEC [SSISDB].[catalog].[set_execution_parameter_value] @ETLID, 30, N'Ruta', @RutaParam;
  
PRINT 'Ejecutando el Paquete'

  EXEC [SSISDB].[catalog].[start_execution] @ETLID