skip to Main Content

Put GetOrdinal Method to Good Use

How often do you see DataReader code that looks like this?

using (IDataReader dr = cmd.ExecuteNonQuery())
{
    while (dr.Read())
    {
        int orderId = dr.GetInt32(0);
        string customerId = dr.GetString(1);
        int employeeId = dr.GetInt32(2);
        DateTime orderDate = dr.GetDateTime(3);
        double freight = dr.GetDouble(4);

        // do stuff
    }

    dr.Close();
}

If that looks like your code, you are not alone :-). Try searching codesearch.google.com for the following:

lang:c# (reader|dr)\.GetInt32\(

codesearch google

With the above code, anytime the order of columns in the SQL statement or stored procedure changes, the code is broken. And if you have lots of columns to read from, it’s a real nightmare to maintain the indexes.

The next time you write another DataReader loop, consider doing it this way instead:

using (IDataReader dr = cmd.ExecuteNonQuery())
{
    int ORDER_ID = dr.GetOrdinal("OrderID");
    int CUSTOMER_ID = dr.GetOrdinal("CustomerID");
    int EMPLOYEE_ID = dr.GetOrdinal("EmployeeID");
    int ORDER_DATE = dr.GetOrdinal("OrderDate");
    int FREIGHT = dr.GetDecimal("Freight");

    while (dr.Read())
    {
        int orderId = dr.GetInt32(ORDER_ID);
        string customerId = dr.GetString(CUSTOMER_ID);
        int employeeId = dr.GetInt32(EMPLOYEE_ID);
        DateTime orderDate = dr.GetDateTime(ORDER_DATE);
        double freight = dr.GetDouble(FREIGHT);

        // do stuff
    }

    dr.Close();
}

Using GetOrdinal makes the code much more readable and maintainable. You are calling GetOrdinal just once for each column, any performance penalty is insignificant compared to the benefits. Be careful not to put the GetOrdinal code inside the while block as that will unnecessarily slow you down (about 3% according to this article).

kick it on DotNetKicks.com

Finds of the Week – February 3, 2007

.NET

Software/Tools

Windows Mobile – Pocket PC

  • Looks like Google has fixed the problems that were plaguing Gmail IMAP on Windows Mobile devices. Read the blog entry from the official Gmail Blog here. And here’s a related article from PC World: Google fixes Gmail IMAP problem on Windows Mobile.
  • Viigo is a free RSS reader for Windows Mobile. I have not really used it that much but it seems to work ok.Viigo RSS Reader for Windows Mobile
  • Skyfire is another promising Windows Mobile browser currently in private beta. Via Engadget.
  • If you have a Samsung Windows Mobile device, try the included PicSel browser. Here’s a review of Picsel browser, with a lot of useful how-to information included. By Tam Hanna.

And Now, For Something A Little Different

Nano

Finds of the Week – January 27, 2008

.NET Programming, C#

Powershell

Software and Tools

Windows Mobile / Pocket PC

Something Different

Finds of the Week – January 20, 2008

.NET, C#, Programming

Software and Tools

  • Jeff Atwood shared the Top Five Browser Shortcuts Everyone Should Know. I didn’t know about the middle mouse button browser shortcut. Very useful.
  • Reading Nazmul Idris’ post on Microsoft OneNote 2007 prompted me to try it myself…. and I loved it. It’s a great application to keep track of notes, journal entries, work logs, etc. It’s going to be an essential app for me from now on.

    OneNote 

    I especially like offline mode support for USB flash drives. You can keep your Notebooks on a flash memory card. OneNote automatically synchronizes its local cache with the flash card when the card is inserted. When not inserted, you can still work on the local cached copy.

    What I don’t like: no VBA macro support. One of the first thing I tried was pressing ALT+F11 to bring up the VBA IDE… nothing happened. A quick search in Online Help confirmed my sinking feeling: no VBA support. Supposedly, you can write add-ins.

Windows Mobile/Pocket PC

Gadgets

Something different

  • Watch dolphins blowing circles made of air bubbles.

Speed Up Gmail IMAP by Working Offline

One of the best tricks to speed up Gmail IMAP access is to use offline mode in your email application. Offline mode is supported by popular email clients such as Microsoft Outlook 2007, Outlook Express (Windows XP), Windows Mail (Windows Vista), and Thunderbird.

Work Offline menu in Windows Mail

While you are in offline mode, your actions are carried out locally and the sequence is remembered by the email client. Later on, when you get back online, the same actions are then executed on the server. Offline mode is much faster (think instantaneous vs. several seconds for each action) than online mode because the email client does not have to connect/talk to the IMAP server each time you do something.

Windows Mail Performing offline tasks...

Outlook 2007’s Offline Support

Not all offline modes are created equally however. One would think that Microsoft top-of-the-line email client Outlook (2007) would have better support for working offline than its little brothers Outlook Express and Windows Mail, but that’s just not the case. According to my own testing, Outlook 2007 is the worst among the three when it comes to supporting offline access. Specifically, you cannot copy or move messages in Outlook 2007 when working offline.

Microsoft Office Outlook: The connection to the server is unavailable. Outlook must be online or connected to complete this action.

If you have read my article Gmail IMAP Tips article, you know Gmail IMAP is all about moving and copying messages. Lacking the ability to move/copy messages in offline mode reduces the usefulness of Outlook as a Gmail IMAP client by about oh… 90% right there for me! I have not tested Thunderbird but it think I does support moving/copying images while working offline.

My Current IMAP Client

I still use Outlook for its calendar and to synchronize with my i730 Windows Mobile phone, but for Gmail IMAP, Windows Mail (Windows Vista) is now my email client of choice.

If anyone reading this is from the Microsoft Outlook team, please fix Outlook!

Related Articles

Back To Top