【问题标题】:Run AS400 stored procedure from C# throw an error从 C# 运行 AS400 存储过程抛出错误
【发布时间】:2016-01-17 11:40:35
【问题描述】:

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

附加信息:错误 [42S02] [IBM][System i Access ODBC 驱动程序][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 版

编辑: 我将 sql 变量更改如下:

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

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

【问题讨论】:

  • 查找您的连接字符串。文件名错误。您没有发布连接字符串所在的代码。
  • 检查存储过程是否调用了文件。
  • 我不清楚,你能解释一下吗?
  • 您在问题中显示AS400 options for the stored procedure。那是从哪里来的?您是否使用 Data Studio 或其他客户端来查看这些选项?第一个选项是Max number of result sets: 0。所以似乎没有定义result set。您能显示 CREATE PROCEDURE 语句中的创建属性吗?

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


【解决方案1】:

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

【讨论】:

  • 查询是一个简单的 select * from MyLibrary.TABLE1; TABLE1 肯定存在
  • 返回原始 SQL。问题不在于您的 VS 应用程序。问题出在数据库过程中。
  • 那你有什么建议?
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-19
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多