Create a C# Assembly
The key to getting all aspects of an AS/400 stored to work is to call it from a C# assembly. Since C# cannot be used directly from SSIS 2005 (this should change for SSIS 2008), a minimal amount of VB wrapper script needs to be written in SSIS to call the C' assembly. The process of creating a C# assembly in general is described in this article. Here, I specialize a little and show you how to call an AS/400 stored procedure.
Create a C# Class Library project called AS400. Rename the default class name is somethig useful like API. Create a keyfile (see here on how this is done) and add this to your project.
Create the connection object
Add the "using System.Data.OleDb;" declaration to your C# class. The connection object is instantiated in the constructor using the crude connection string approach. This is what the class looks like so far:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Data.OleDb;
- namespace AS400
- {
- public class API
- {
- private string sConnectionString;
- // Default: Development environment
- public API()
- {
- sConnectionString = "Provider=IBMDA400.DataSource.1;Data Source=SERVERD;Persist Security Info=True;Password=*****;User ID=MFLXODBC;Initial Catalog=SERVERD;Catalog Library List=MFLXDATA";
- con.ConnectionString = sConnectionString;
- con.Open();
- }
- // SERVERD, MFLXODBC, FIGBUSH07, MFLXDATA
- public API(string sServer, string sUser, string sPasswd, string sCatalog)
- {
- // Make up connection string
- sConnectionString = "Provider=IBMDA400.DataSource.1;Data Source="+sServer+";Persist Security Info=True;Password="+sPasswd+";User ID="+sUser+";Initial Catalog="+sServer+";Catalog Library List=" + sCatalog;
- con.ConnectionString = sConnectionString;
- con.Open();
- }
- . . .
Declare parameters to the AS/400 Stored Procedure
For simplicity's sake, this stored procedure has only one INOUT parameter, which is passed back out of the public class method. Do the same for all the other parameters of your stored procedure and remember to set accurately state the data types and parameter direction.
- public void StoredProc(
- ref string sReturnCode // OK if success, else AS400 return code
- )
- {
- cmd.Connection=con;
- cmd.CommandType=System.Data.CommandType.StoredProcedure;
- // In the form: LibraryName.ProcedureName
- cmd.CommandText = "ACSLPGM.ALEQXFK";
- try
- {
- // Executes the equivalent of "call ACSLPGM.ALEQXFK('')"
- cmd.Parameters.Add("@RETURNCODE", OleDbType.Char, 7);
- cmd.Parameters["@RETURNCODE"].Value = "";
- cmd.Parameters["@RETURNCODE"].Direction = ParameterDirection.InputOutput;
Execute the Stored Procedure and collect returned parameter values
Continuing with the code above, our stored procedure is executed using the OleDbCommand.ExecuteNonQuery() method:
- . . .
- cmd.ExecuteNonQuery();
- try
- {
- // Get the return parameters
- sReturnCode = cmd.Parameters["@RETURNCODE"].Value.ToString();
- }
- catch (Exception ex1)
- {
- sReturnMessage = ex1.Message.ToString();
- }
- }
- catch (Exception ex2)
- {
- sReturnMessage = ex2.Message.ToString();
- }
- } // ...StoredProc
- } // ...class API
- } // ...namespace AS400
Collecting recordset data from a stored procedure
If the store procedure returns a record set, then you would call it differently and collect the resulting data and collect the data as follows:
(TODO)
Build and deploy the C# Assembly to the GAC
Ctrl-Shift-B. Simple.