Define the following Class:
using System;
using
System.Data;
using
System.Data.Odbc;
namespace
ConnectToAS400
{
public class Db2Access
{
#region Private members
private
readonly string
_server;
private
readonly int
_portNumber;
private
readonly string
_database;
private
readonly string
_userName;
private
readonly string
_password;
private
readonly string
_connectionString;
#endregion
#region Constructor
public
Db2Access(string server, int portNumber, string
database, string userName, string password)
{
_server = server;
_portNumber = portNumber;
_database = database ;
_userName = userName ;
_password = password;
_connectionString = String.Format("Driver={{IBM
DB2 ODBC DRIVER}};Database={0};Hostname={1};Protocol=TCPIP;Port={2};Uid={3};Pwd={4};",
_database,
_server,
_portNumber,
_userName,
_password);
}
#endregion
#region Public methods
public DataTable ReadDb2Table(string
schemaName, string tableName, string filter)
{
try
{
DataTable
db2Data = new DataTable();
string query
= String.Format("Select
* from {0}.{1}", schemaName, tableName);
if
(!string.IsNullOrEmpty(filter))
{
query = String.Format("{0}
where {1}", query.Trim(), filter);
}
using
(OdbcConnection c = new
OdbcConnection(_connectionString))
{
c.Open();
using
(OdbcCommand selectCMD = new OdbcCommand(query))
{
selectCMD.Connection =
c;
OdbcDataReader r = selectCMD.ExecuteReader();
db2Data.Load(r);
}
c.Close();
}
return
db2Data;
}
catch
(Exception ex)
{
throw
ex;
}
}
#endregion
}
}
The Code
private
void button2_Click(object
sender, EventArgs e)
{
Db2Access
thing = new Db2Access("MYSERVER", 50000, "MYDATABASE", @"MYDOMAIN\MYUSERNAME",
"MYPASSWORD");
DataTable
db2Data = thing.ReadDb2Table("THESCHEMANAME",
"THETABLENAME", "");
}