Pages

Friday, July 27, 2012

Sample Data Models

Found an interesting website which contains sample data models for many scenarios. This could be a great learning and start up resource.

http://www.databaseanswers.org/data_models/

Friday, May 18, 2012

Get details from Active Directory


The System.DirectoryServices namespace provided classes for access to Active Directory (AD) and allowed us to perform different operations on AD. But it required the knowledge of the AD structure and Principal Store.

In .Net framework 3.5, Microsoft introduced the System.DirectoryServices.AccountManagement namespace that provides uniform access and manipulation of user, computer, and group security principals across the multiple principal stores: Active Directory Domain Services (AD DS), Active Directory Lightweight Directory Services (AD LDS), and Machine SAM (MSAM).

System.DirectoryServices.AccountManagement manages directory objects independent of the System.DirectoryServices namespace and does not require knowledge of Principal Store.

Managed directory services applications can take advantage of the AccountManagement API to simplify management of user, computer and group principals. Solutions that previously required intricate knowledge of the store or lengthy code, such as finding all groups to which a user belongs, are accomplished in a few lines of code with theAccountManagement API.

Sample code to get the details of Group and Users is shown below:




 public class ADUtility
 {
    private string domain = string.Empty;
    public ADUtility(string domain)
    {
       this.domain = domain;
    }
    
    private PrincipalContext GetPrincipalContext()
    {
       PrincipalContext oPrincipalContext
                        new PrincipalContext(ContextType.Domain, domain);

       return oPrincipalContext;
    }

    public GroupPrincipal GetGroup(string groupName)
    {
       PrincipalContext oPrincipalContext = GetPrincipalContext();
       GroupPrincipal groupPrincipal
                      GroupPrincipal.FindByIdentity(oPrincipalContextgroupName);
       
       return groupPrincipal;
    }

    public UserPrincipal GetUser(string userName)
    {
        PrincipalContext oPrincipalContext = GetPrincipalContext();
        UserPrincipal oUserPrincipal
                      UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);

        return oUserPrincipal;
    }
 }

Sunday, April 29, 2012

Calculate product of given numbers in SQL server



The below example shows how to find the product of values in one column of table

declare @values table (value float)
declare @product float


insert into @values values (1)
insert into @values values (-3)
insert into @values values (2)
insert into @values values (6)


set @product = 0


select @product = exp(sum(log(abs(value)))) from @values


if ((select count (1) from @values where value < 0) % 2) =1
begin
  select @product = @product * (-1)
end


select @product


Thursday, March 15, 2012

DescriptionAttribute for Enums in C#

Enums provides an efficient way to define a set of named constants that may be assigned to a variable. In C# they can be only number based and have usual naming limitation of not allowing special characters or spaces. Many times we have a requirement where we need to have display user friendly name for Enum value on screen.
public enum ProjectPhase
{
    RequirementsGathering = 1,
    AnalysisDesign = 2,
}
In the above example we need to display more meaningful values on screen. This can be achieved by using the System.ComponentModel and System.Reflection namespace.

Step 1: Use the DescriptionAttribute class to provide the friendly name for enum values.
using System.ComponentModel;
public enum ProjectPhase
{
    [Description("Requirements Gathering")]
     RequirementsGathering = 1,
    [Description("Analysis and Design")]
     AnalysisDesign = 1,
}

Step 2: Create an static class which contains an extension method that extends the Enum type. This extension method GetDecription returns the description of the provided enum value. If there is no description attribute applied on the enum value then the string representation of the enum value is returned.
public static class EnumExtensions
{
    public static string GetDescription(this Enum value)
    {
        string description = string.Empty;
        FieldInfo field = value.GetType().GetField(value.ToString());
        DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false);
        if (attributes.Length > 0)
            description = attributes[0].Description;
        else
             description = value.ToString();
        return description;
     }
}

Step 3: Accessing the Description of enum value
Console.WriteLine(ProjectPhase.Code.GetDescription());