Wednesday, May 25, 2016

Call external DLL from c# using reflection

using System;
using System.Collections.Generic;
using System.Reflection;
using DTOTest;

namespace TestLoadDLL
{
    class Program
    {
        public static string dllRoot = @"......\DLLTest.dll";
        public static string assemblyName = "DLLTest.GenerateERP";
        public static string methodName = "AddInfo";
        static void Main(string[] args)
        {
            List lstObjects = new List();
            lstObjects.Add(new ObjectDTO());
            lstObjects.Add(new ObjectDTO());
            lstObjects.Add(new ObjectDTO());
            lstObjects.Add(new ObjectDTO());

            Assembly a = Assembly.LoadFile(dllRoot);
            Type type = a.GetType(assemblyName);

            if (type != null)
            {
                MethodInfo methodInfo = type.GetMethod(methodName);
                object classInstance = Activator.CreateInstance(type, null);
                if (methodInfo != null)
                {
                    var result = (bool)methodInfo.Invoke(classInstance, new object[]
                        {
                            lstObjects
                        });
                }
            }

        }
    }
}


------------------------------------------------

using DTOTest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DLLTest
{
    public class GenerateERP : IGenerateERP
    {
        public bool AddInfo(List lstObjects)
        {
            if (lstObjects.Count > 1)
                return true;
            else
                return false;
        }
    }
}

Tuesday, February 3, 2015

Rezolvare problema lui Danezu



create table Datetimes
(
       Date1 datetime,
       Date2 datetime,
       Date3 datetime,
       Date4 datetime,
       Date5 datetime
)

declare @i int
set @i = 1000

while (@i>0)
begin
       insert into Datetimes values(DATEADD(day, (ABS(CHECKSUM(NEWID())) % 65530), 0),DATEADD(day, (ABS(CHECKSUM(NEWID())) % 65530), 0),DATEADD(day, (ABS(CHECKSUM(NEWID())) % 65530), 0),DATEADD(day, (ABS(CHECKSUM(NEWID())) % 65530), 0),DATEADD(day, (ABS(CHECKSUM(NEWID())) % 65530), 0))
       set @i = @i - 1
end


go

select
max(Date) from
(
select max(Date1) a1 ,max(Date2) a2, max(Date3) a3, max(Date4) a4, max(Date5) a5 from Datetimes
)
t
unpivot
(
       Date for Dates in ([a1],[a2], [a3],[a4],[a5])
) as UnPvt

Pivot / Unpivot


create table Cart
(
    Product varchar(32),
    Color varchar(32),
    Nr int
)

insert into Cart values ('car','blue',3)
insert into Cart values ('car','red',2)
insert into Cart values ('car','black',3)
insert into Cart values ('car','white',1)
insert into Cart values ('car','blue',1)
insert into Cart values ('carrot','blue',3)
insert into Cart values ('carrot','red',2)
insert into Cart values ('onion','black',3)
insert into Cart values ('onion','white',1)
insert into Cart values ('car','blue',1)
insert into Cart values ('beer','blue',3)
insert into Cart values ('car','red',2)
insert into Cart values ('car','black',3)
insert into Cart values ('beer','white',1)
insert into Cart values ('car','blue',1)
insert into Cart values ('carrot','blue',3)
insert into Cart values ('beer','red',2)
insert into Cart values ('onion','black',3)
insert into Cart values ('onion','white',1)
insert into Cart values ('car','blue',1)
insert into Cart values ('car','blue',1)
insert into Cart values ('beer','blue',3)
insert into Cart values ('car','red',2)
insert into Cart values ('beer','black',3)
insert into Cart values ('beer','white',1)
insert into Cart values ('car','blue',1)
insert into Cart values ('car','blue',3)
insert into Cart values ('beer','red',2)



select * into CartPivot  from Cart
pivot
(
    sum(Nr)
    for Color in ([blue],[black],[white],[red])
) PivotTable



select * from CartPivot


select Product, Nr, details from CartPivot
Unpivot
(
  Nr for details in  ([blue],[black],[white],[red])
) as UnPvt













Wednesday, October 15, 2014

Sql Server perform pagination on the returned results

Variant 1 (Sql server 2008 and upper)

SELECT * FROM
(
      SELECT *,
        ROW_NUMBER() OVER (ORDER BY PK_ID) as rowNum
      FROM  WHERE Data_rap > '2014-01-01'
) sub
WHERE rowNum > 9
  AND rowNum <= 9 + 20

 
Limitations
 - make a subquery
 - ORDER BY is mandatory

Variant 2 (Sql server 2012 and upper)

SELECT * FROM WHERE Data_rap > '2014-01-01'
ORDER BY PK_ID OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY;


Limitations

 - ORDER BY is mandatory to use OFFSET and FETCH clause.

 - OFFSET clause is mandatory with FETCH. You can never use, ORDER BY … FETCH.

 - TOP cannot be combined with OFFSET and FETCH in the same query expression.

 - The OFFSET/FETCH rowcount expression can be any arithmetic, constant, or parameter expression that will return an integer value. The rowcount expression does not support scalar sub-queries.

Thursday, September 25, 2014

Double check singleton design pattern

public class MySingleton {
    private static object myLock = new object();
    private static volatile MySingleton mySingleton = null; // 'volatile' is unnecessary in .NET 2.0 and later
 
    private MySingleton() {
    }
 
    public static MySingleton GetInstance() {
        if (mySingleton == null) { // 1st check
            lock (myLock) {
                if (mySingleton == null) { // 2nd (double) check
                    mySingleton = new MySingleton();
                    
                }
            }
        }

        return mySingleton;
    }
}

Friday, May 16, 2014

Get SQL Server user's roles

SELECT
    p.NAME, m.NAME
FROM sys.database_role_members rm
JOIN sys.database_principals p ON rm.role_principal_id = p.principal_id
JOIN sys.database_principals m ON rm.member_principal_id = m.principal_id

Tuesday, October 8, 2013

IIS Windows Authentification Web Site Keeps Asking for Credentials

Problem:
Although the provided credentials are valid, IIS keeps prompting for credentials.

Solution:
 Disable the loopback check by setting the DisableLoopbackCheck registry key.

To set the DisableLoopbackCheck registry key, follow these steps:
  1. Set the DisableStrictNameChecking registry entry to 1.
  2. Click Start, click Run, type regedit, and then click OK.
  3. In Registry Editor, locate and then click the following registry key:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa

  4. Right-click Lsa, point to New, and then click DWORD Value.
  5. Type DisableLoopbackCheck, and then press ENTER.
  6. Right-click DisableLoopbackCheck, and then click Modify.
  7. In the Value data box, type 1, and then click OK.
  8. Quit Registry Editor, and then restart your computer.

Source: http://support.microsoft.com/kb/896861