0

I am trying to run a stored procedure from my code and I am getting the following error:

Additional information: ERROR [42S02] [IBM][System i Access ODBC Driver][DB2 for i5/OS]
SQL0204 - StoredProc1 in MyLibrary type *FILE not found.

My code:

internal DataTable Retrieve()
{
        var sql = string.Format("Select * from StoredProc1");
        DataSet dataset = new DataSet();
        OdbcCommand command = new OdbcCommand(sql);

        // MyConnectionString = "ODBC;DATABASE=MyLibrary;DSN=AS400-MyLibrary;UID=MyUser;PWD=MyPwd;ALLOWUNSCHAR=0;"
        // It works fine for sure since I can change the StoredProc1 to a table instead and the query works fine. So it is not a connection problem.
        command.Connection = _libraryConnection.Connection;
        command.CommandType = CommandType.StoredProcedure;

        OdbcDataAdapter adapter = new OdbcDataAdapter(command);

        lock (_anyObj)
        {
            _libraryConnection.OpenConnection();
            adapter.Fill(dataset);                
            _libraryConnection.CloseConnection();
        }

        return dataset.Tables[0];
}

AS400 stored procedure SQL:

BEGIN
DECLARE C2 CURSOR WITH RETURN FOR

SELECT * FROM MyLibrary.TABLE1;

OPEN C2 ;
END 

AS400 options for the stored procedure:

Max number of result sets: 0
Data access: Read SQL Data
Concurrent access resolution: Default
Transaction control: Do not commit on return
Unified debugger mode: Disallow debug mode

AS400 version 7 release 1

EDITED: I changed the sql variable as following:

var sql = "{CALL StoredProc1()}";

Now it is not throwing exception but in other hand I do not get any rows in the datatable. The queried table contains records for sure.

4
  • look for your connection string. The filename is wrong. You didn't post the code where the connection string is located.
    – jdweng
    Jan 17, 2016 at 11:55
  • Check the stored procedure to see if it calls a file.
    – jdweng
    Jan 17, 2016 at 13:04
  • I am not clear, can you explain?
    – ehh
    Jan 17, 2016 at 13:05
  • 1
    You show AS400 options for the stored procedure in your question. Where does that come from? Are you using Data Studio or some other client to see those options? The first option says Max number of result sets: 0. So it seems that there is no result set defined. Can you show the creation attributes from the CREATE PROCEDURE statement? Jan 18, 2016 at 0:20

2 Answers 2

0

Look in database for the procudure : StoredProc1. It is opening a file that doesn't exist.

3
  • The query is a simple select * from MyLibrary.TABLE1; And TABLE1 exists for sure
    – ehh
    Jan 17, 2016 at 13:33
  • Go back to original SQL. The problem is not with your VS application. The problems is in the database procedure.
    – jdweng
    Jan 17, 2016 at 15:05
  • So what is your suggestion?
    – ehh
    Jan 18, 2016 at 5:44
0

To make it works, need to do as following:

2 different connection options:

Using ODBC Connection

internal DataTable Retrieve()
{
    var sql = "CALL STOREDPROC1()";  // THOSE ARE THE POSSIBLE SYNTHAXES
                                    // OR var sql = "{CALL STOREDPROC1()}";
    DataSet dataset = new DataSet();
    OdbcCommand command = new OdbcCommand(sql);

    command.Connection = _libraryConnection.Connection;
    command.CommandType = CommandType.StoredProcedure;
    command.CommandTimeout = 0;  // OPTIONAL

    OdbcDataAdapter adapter = new OdbcDataAdapter(command);

    lock (_anyObj)
    {
        _libraryConnection.OpenConnection();
        adapter.Fill(dataset);                
        _libraryConnection.CloseConnection();
    }

    return dataset.Tables[0];

}

Using ADODB Connection

internal DataTable Retrieve()
{
    var sql = "STOREDPROC1()";  // THIS IS THE SYNTHAX
    OleDbDataAdapter adapter= new OleDbDataAdapter();
    DataTable dt = new DataTable();
    ADODB.Command command = new ADODB.Command();
    ADODB.Recordset rs = new ADODB.Recordset();

    command.ActiveConnection = _libraryConnection.Connection;
    command.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc;
    command.CommandText = sql;
    command.CommandTimeout = 0;  // OPTIONAL

    rs.CursorLocation = ADODB.CursorLocationEnum.adUseServer;

    lock (_anyObj)
    {
        rs.Source = command;
        rs.Open();
        adapter.Fill(dt, rs);
        rs.Close();
    }       

    return dt;

}

AS400 options for the stored procedure:

Max number of result sets: 1
Data access: Read SQL Data
Concurrent access resolution: Default
Transaction control: Do not commit on return
Unified debugger mode: Disallow debug mode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.