Archive for the ‘.NET Development’ Category.

ODAC Released with Entity Framework and LINQ Support

Oracle Data Access Components Released with Entity Framework and LINQ Support

The production version of ODAC 11.2 Release 4 (11.2.0.3) has arrived for Entity Framework and LINQ developers! It includes 32-bit and x64 versions with the option of using an automated installer or xcopy deployment.

The release incorporates Oracle Database client 11.2.0.3, which can access Oracle Database 9.2 and higher. Oracle supports Entity Framework and LINQ with Microsoft Visual Studio 2010 and .NET Framework 4, including Entity Framework 4.1 and 4.2.

ODAC New Features List

Getting User Information For all the users in an Active Directory Group.

A recent requirement came up for a quick bit of code to retrieve the names and e-mail addresses from an ActiveDirectroy group. I did this in code instead of power shell in order to be able to use these methods in further logic.

So here we go….

First we’re going to create a simple structure to represent the user information we need:

        public struct UserInfo
       
{
            public string DisplayName;
            public string Mail;
            public string Login;
        }

Next were going to create a simple entry point in which we will use to test our methods:

static void Main(string[] args)
{
    ArrayList ar = GetADGroupUsers("GroupName");

    List<UserInfo> users = new List<UserInfo>();
    foreach (string user in ar)
    {
        users.Add(GetADUserInfo(user));

    }
}

 

Now we will need to implement the two methods seen above. First the method to get all the users in a group:

 

static public ArrayList GetADGroupUsers(string groupName)
{
    SearchResult result;
    DirectorySearcher search = new DirectorySearcher();
    search.Filter = String.Format("(cn={0})", groupName);
    search.PropertiesToLoad.Add("member");
    search.PropertiesToLoad.Add("mail");
    search.PropertiesToLoad.Add("samaccountname");
    result = search.FindOne();

    ArrayList userNames = new ArrayList();
    if (result != null)
    {
        for (int counter = 0; counter <
                 result.Properties["member"].Count; counter++)
        {

            userNames.Add((string)result.Properties["member"][counter]);
        }
    }
    return userNames;
}

Now that we have the ArrayList of users, we will need to get the information about the users:

static public UserInfo GetADUserInfo(string userName)
{
    SearchResult result;

    DirectoryEntry de = new DirectoryEntry("LDAP://mydomaincontroller/"+userName);

    UserInfo ui = new UserInfo();
    ui.DisplayName = de.Properties["displayName"].Value.ToString();
    ui.Mail = de.Properties["mail"].Value.ToString();
    ui.Login = de.Properties["sAMAccountName"].Value.ToString();
    return ui;

}

And that’s it, now we have a nice list of users with their email addresses.

A great place to get started with .NET Micro Framework

I have long been an advocate of GHI Electronics and their development systems for use in my tinkering.

Today they announced something that I am going to add to my must have tinkering list.

The FEZ Spider Starter kit is now on the website for pre-order.

On a high level it includes:

So for anyone looking to get starting with device building, robotics and/or tinkering this is a great place to begin.

And the best part.. the current price is only $250.

What Version of Visual Studio Am I Really Using?

It’s really quite simple. From the tool bar, open the help menu and select about visual studio…

image

Now the reason that I bring this up today, is that Visual Studio LightSwitch, being released today, requires SP1. And if you’re anything like me, you’ve long forgotten if you installed it.

To .Close() or .Dispose() on a SqlConnection

So it has been asked here around the office and I’ve heard it in many discussions. So I decided I would do a deep dive into when and why to use each.

For the code examples, these are taken from .Net Reflector Pro. The framework version I will be using is .Net Framework 4.

First lets look at a simple T-SQL Operation using a SqlConnection and SqlCommand object. We will also be implementing the using statement.

So our code example starts out with a simple SELECT statement that retrieves the hostname from the database server.

using (SqlConnection oConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI"))
{
SqlCommand oCmd = new SqlCommand("Select HOST_NAME()", oConn);
oCmd.CommandType = System.Data.CommandType.Text;

oConn.Open();

string hostName = oCmd.ExecuteScalar().ToString();
}

Now in implementing this, it would appear as though we are leaving our SqlConnection both open and undisposed. However this is not the case.

As outlined in the documentation for the using statement. The using statement ensures the correct use of IDisposable objects. So what is actually happening is that when your code is leaving the using block the Dispose method is being called. So lets take a quick gander at the Dispose method of the SqlConnection object.

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}

Now you’ll notice that the Dispose method contains a parameter of disposing. However when the Dispose() method is called from the using statement no parameter is passed. So lets actually look at the base dispose method. For this we actually have to go down to the base class of System.ComponentModel.Component.

public void Dispose()
{
    this.Dispose(true);
    GC.SuppressFinalize(this);
}

So here is where the magic happens, so you see that when the Dispose method is called, it turns around and calls the Dispose() method with the true parameter. When the Dispose method is called with a true parameter [Dispose(true)] the User Connection Options and Pool Group are both set to null, and then the connection is closed, and then, the resources are released.
In summary, explicitly calling close before dispose, implements double work. It shows up in some patterns and design books as being the proper way to handle this scenario, however it is not.
So when do we use the close, we’ll that’s simple, when we need to reopen the connection later, in an enumeration for example.