Monday, March 12, 2012

Deleting duplicate rows on table with/without primary key in Sql Server


Table with primary key
Id    name
1                     Harpreet
2                     Harpreet
3                     Harpreet
4                     John
5                     Smit
select min(id) from #tablewithprimary group name
=1,4,5
Delete from .. where id not in (1,4,5)
Result : 1,4,5
delete from #tablewithprimary where id not in   
  (select min(id) from #tablewithprimary group by course,subject) 

C# Lock


lock ensures that one thread does not enter a critical section while another thread is in the critical section of code. If another thread attempts to enter a locked code, it will wait (block) until the object is released.
The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock. This statement takes the following form:
 
Object thisLock = new Object();
lock (thisLock)
{
    // Critical code section
}

class Program
    {
        static void Main(string[] args)
        {
            Thread[] threads = new Thread[10];
            Account acc = new Account(1000);
            for (int i = 0; i < 10; i++)
            {
                Thread t = new Thread(new ThreadStart(acc.DoTransactions));
                threads[i] = t;
            }
            for (int i = 0; i < 10; i++)
            {
                threads[i].Start();
            }
            Console.ReadKey();
        }
    }

    class Account
    {
        int balance;

        Random r = new Random();

        public Account(int initial)
        {
            balance = initial;
        }

        int Withdraw(int amount)
        {

            // This condition will never be true unless the lock statement
            // is commented out:
            if (balance < 0)
            {
                throw new Exception("Negative Balance");
            }

            // Comment out the next line to see the effect of leaving out
            // the lock keyword:
            lock (this)
            {
                if (balance >= amount)
                {
                    Console.WriteLine("Balance before Withdrawal :  " + balance);
                    Console.WriteLine("Amount to Withdraw        : -" + amount);
                    balance = balance - amount;
                    Console.WriteLine("Balance after Withdrawal  :  " + balance);
                    return amount;
                }
                else
                {
                    return 0; // transaction rejected
                }
            }
        }

        public void DoTransactions()
        {
            for (int i = 0; i < 100; i++)
            {
                Withdraw(r.Next(1, 100));
            }
        }
    }

How to get the current assembly path?


*** Program is the name of the class 
string assemblyPath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
Console.WriteLine(assemblyPath);
Console.ReadKey();

Friday, March 2, 2012

How to use relative paths for an Assembly in the C# projects in order to avoid the conflicts with the references?

Let us consider the following hierarchy of the project



Let us say the working project name is WorkingProject . Follow the following steps in order to use relative paths for the assemblies

1. Add the references to the project (LocalFunctions.dll and LocalClasses.dll) by browsing. To Add reference to a project, right click on the reference folder and click on add reference.

2. Open the physical location of the WorkingProject

3. Edit the .csproj file in notepad++ or notepad.

4. Look for the HintPath tag for each and every reference and check the required relative paths under this tag value.

E.g. in this case it should be like this


a)      ..\..\ LocalSolution \ LocalFunctions \bin\Debug\LocalFunctions.dll
b)      ..\..\ LocalSolution \ LocalClasses\ bin\Debug\ LocalClasses.dll



Note: By default these relative paths will be populated based on the added references w.r.t. current working directly of the project.

If you need any windows assembly use path like

C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll