从 C# 运行 AS400 存储过程引发错误



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

其它信息: 错误 [42S02] [IBM][System i Access ODBC Driver][DB2 for i5/OS]
SQL0204 - 在 MyLibrary 类型中存储 Proc1 *找不到文件。

我的代码:

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 版

编辑:我更改了 sql 变量,如下所示:

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

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

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

要使其正常工作,需要执行以下操作:

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

相关内容

  • 没有找到相关文章

最新更新