.NET Journey – The First Steps…
Okay, so you’ve decided that you want to learn .NET and techniques related to connecting to the iSeries. So do I! For this blog, we’re going to concentrate on C#. Why C# over the over available .NET languages? It’s because I have more experience with C/C++ then I do with Visual Basic or Java. I’m sure that the examples presented in this blog can be easily modified to work with any .NET language.
First you need to get the tools. To start with, click here to download Visual Studio 2005 Express C# Edition. As of this writing, VS 2008 is available but all my examples will be created with VS 2005. Feel free to try VS 2008 but be aware that there will be differences and I won’t be able to explain why. Then download and install the .NET 2.0 SDK from here. The SDK has command line tools and documentation that wil be extremely helpful.
Also make sure your Windows version is up to date, especially concerning the .NET framework.
Now for the iSeries stuff. You’ll need to be on at least V5R3 of OS/400 and make sure you have the latest service pack for your version of iSeries Access. I know with V5R4 of iSeries Access that you will need to have the latest service pack as a bug in the .NET provider will cause your .NET app to crash upon termination. IBM’s web site doesn’t guarantee that their provider will work with .NET 2.0 but so far I haven’t had any problems.
Once you have all your goodies installed, start up Windows notepad and paste the following code. The only change you’ll need is to change the connection string (“DataSource=xxxxxx”) replacing “xxxxxx” with your system name or IP address.
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();
}
}
}
Then save the file to a location on your PC. For this example I’m going to name the file “iSeriesTester.cs” and place the file in “My Documents”.
Now we’re going to compile and test the application. If you installed the .NET 2.0 SDK correctly then you should find the following option:
Start – All Programs – Microsoft .NET Framework SDK v2.0 – SDK Command Prompt
Once you select this option a DOS command prompt will appear. This prompt sets certain environment attributes for you to easily compile .NET code. Use the “CD” command to change to the directory where you saved the above .cs file. Now enter the following on the command prompt:
csc /r:"C:\Program Files\IBM\Client Access\IBM.Data.DB2.iSeries.dll" iSeriesTester.cs
If everything goes according to plan, the DOS prompt should appear again without any error messages. Now type “iSeriesTester” on the command prompt and press Enter. You should see the contents of QAUOOPT displayed in the prompt window.
Congratulations! You’ve just compiled your first .NET program!
In the next installment of the journey I’ll go into more detail about the program code and the command line option.