Executing an ISeries Stored Procedure from .NET with ODBC

Below is an example of how one might execute a stored procedure on the ISeries from a C# application. I understand that there are certainly other, much shorter ways to achieve this.

using (OdbcConnection DbConnection = new OdbcConnection(ConfigurationManager.ConnectionStrings["MyISeres"].ConnectionString))
using (OdbcCommand DbCommand = new OdbcCommand())
{
    DbCommand.Connection = DbConnection;
    DbConnection.Open();
    DbCommand.CommandText = "call MYLIB.MYPROC(?, ?)";

    OdbcParameter p1= new OdbcParameter();
    p1.DbType = System.Data.DbType.String;
    p1.ParameterName = "MyParm1";
    p1.Size = 50;
    p1.Value = "some value";
    p1.Direction = System.Data.ParameterDirection.Input;
    DbCommand.Parameters.Add(p1);

    OdbcParameter p2= new OdbcParameter();
    p2.DbType = System.Data.DbType.String;
    p2.ParameterName = "MyParm2";
    p2.Size = 50;
    p2.Value = "some other value";
    p2.Direction = System.Data.ParameterDirection.Input;
    DbCommand.Parameters.Add(p2);

    using (OdbcDataReader DbReader = DbCommand.ExecuteReader())
    {
        GridView1.DataSource = DbReader;
        GridView1.DataBind();
    } 
}  

Leave a Reply

Your email address will not be published. Required fields are marked *