从C#运行AS400存储过程会抛出错误

时间:2016-01-17 11:40:35

标签: c# database stored-procedures db2 ibm-midrange

我正在尝试从我的代码运行存储过程,我收到以下错误:

  

其他信息:ERROR [42S02] [IBM] [System i Access ODBC Driver] [DB2 for i5 / OS]
  SQL0204 - 未找到MyLibrary类型* FILE中的StoredProc1。

我的代码:

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存储过程SQL:

BEGIN
DECLARE C2 CURSOR WITH RETURN FOR

SELECT * FROM MyLibrary.TABLE1;

OPEN C2 ;
END 

存储过程的AS400选项:

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版本7第1版

EDITED: 我更改了sql变量如下:

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

现在它不会抛出异常,但另一方面我没有在数据表中获得任何行。查询表包含肯定的记录。

2 个答案:

答案 0 :(得分:0)

在数据库中查找procudure:StoredProc1。它正在打开一个不存在的文件。

答案 1 :(得分:0)

要使其有效,需要执行以下操作:

2种不同的连接选项:

使用ODBC连接

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];

}

使用ADODB连接

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选项:

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