Hallo! Mein Name ist Klaus Mödinger und ich bin Kommanditist der comSID GmbH & Co. KG.
In diesem Artikel zeige ich, wie man mit C# und .NET eine Stored Procedure auf der IBM i (AS/400) aufruft.
Zum Testen erstellt man mit "SQL Prozeduren ausführen" (Bestandteil von IBM i Access Client Solutions) diese Stored Procedure:
create procedure moddat.adder
(
in pSummand1 integer,
in pSummand2 integer,
out pSum integer
)
set pSum = pSummand1 + pSummand2
Die Stored Procedure addiert zwei ganze Zahlen.
Aufgerufen werden kann sie mit diesem C#-Programm:
using System;
using System.Data;
using System.Data.Odbc;
namespace OdbcConsole
{
class Program
{
static void Main(string[] args)
{
int summe = OdbcTest(1, 1);
Console.WriteLine(summe);
Console.ReadLine();
}
private static int OdbcTest(int summand1, int summand2)
{
OdbcConnection con = new OdbcConnection("dsn=as400;uid=user;pwd=pass");
OdbcCommand command = new OdbcCommand("call moddat.adder(?, ?, ?)", con)
{
CommandType = CommandType.StoredProcedure
};
OdbcParameter pSummand1 = new OdbcParameter("pSummand1", OdbcType.Int)
{
Value = summand1,
Direction = ParameterDirection.Input
};
OdbcParameter pSummand2 = new OdbcParameter("pSummand2", OdbcType.Int)
{
Value = summand2,
Direction = ParameterDirection.Input
};
OdbcParameter pSumme = new OdbcParameter("pSumme", OdbcType.Int)
{
Direction = ParameterDirection.Output
};
command.Parameters.Add(pSummand1);
command.Parameters.Add(pSummand2);
command.Parameters.Add(pSumme);
con.Open();
command.ExecuteNonQuery();
command.Dispose();
con.Close();
return (int)pSumme.Value;
}
}
}
Zum Ausprobieren kann man mit Visual Studio eine Konsolen-Applikation erstellen und den Code von oben in das Hauptprogramm pasten. Der ODBC Connection String muss natürlich angepasst werden.
Viel Erfolg!

Kommentar schreiben