.NET Journey – First Example Explained

So…you remember this?

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using IBM.Data.DB2.iSeries;

namespace iSeriesTester
{
    class Program
    {
        static void Main(string[] args)
        {
            String cnnString = "DataSource=xxxxxx";
            bool connected = false;
            iDB2Connection cnn = new iDB2Connection();
            iDB2Command cmd = new iDB2Command();
            cmd.Connection = cnn;
            cnn.ConnectionString = cnnString;
            cmd.CommandText = "select * from qgpl.qauoopt";
            try
            {
                cnn.Open();
                connected = true;
            }
            catch (iDB2ConnectionFailedException)
            {
                MessageBox.Show("Could not connect to system!");
            }
            catch (iDB2CommErrorException e)
            {
                MessageBox.Show(e.MessageDetails);
            }

            if (connected)
            {
                iDB2DataReader reader;
                try
                {
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        try
                        {
                            Console.WriteLine(reader.GetString(0) + " - " + reader.GetString(1));
                        }
                        catch
                        {
                            MessageBox.Show("An error occured retrieving data!");
                        }
                    }
                    reader.Close();
                }
                catch (iDB2SQLErrorException sqlE)
                {
                    MessageBox.Show(sqlE.Message);
                }
            }

            cnn.Close();
            cmd.Dispose();
        }

    }
}

So let’s look at this program in detail. First off is the “using” statement. In .NET a using means which namespace in the .NET framework you’ll be using in your program. It’s not really an “include” like C/C++ or Java. AAMOF, if you search your computer for “System.Windows.Forms” you’ll only find a reference to a .DLL and .TLB.

So how do you know which namespace to include? In the “Microsoft .NET Framework SDK v2.0″ program group there’s an option for “Documentation”. If you open the documentation and search for “String Class” you’ll find that it’s a part of the “System” namespace. So if you want to use the “String” class you’ll need to have a “using System;” line. Also note the “IBM.Data.DB2.iSeries” using statement. If you search your computer for matching files you’ll find a reference to IBM.Data.DB2.iSeries.dll in “C:\Program Files\IBM\Client Access”.

The next line:

namespace iSeriesTester

Defines the namespace for the program. This can be anything but it’s suggested that you organize your program’s namespace into a hierarchy. For example, I could have used the namespace:

namespace InnovativeSystems.NETJourney.iSeriesTester

The “class Program” line is required in C# but doesn’t merit much discussion now. In C# (as in C or C++) there must be a “main” function in each program. In C# your main function will receive an array of strings which equate to the command line options, if any, supplied to your program.

Notice the iDB2Command and iDB2Connection objects declared in the “main” function. These are defined in the “IBM.Data.DB2.iSeries” namespace. The iDB2Command stores the actual SQL statement to be executed and iDB2Connection defines which system we’ll execute the statement against. Once the command is created we attach the connection to the command via “cmd.Connection = cnn;” statement.

The next two lines:

cnn.ConnectionString = cnnString;
cmd.CommandText =

Comments are closed.