[英]Run AS400 stored procedure from C# throw an error
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]
附加信息:错误[42S02] [IBM] [系统i Access ODBC驱动程序] [DB2 for i5 / OS]
SQL0204 - StoredProc1 in MyLibrary type *FILE not found.SQL0204-MyLibrary中的StoredProc1类型* FILE未找到。
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: AS400存储过程SQL:
BEGIN
DECLARE C2 CURSOR WITH RETURN FOR
SELECT * FROM MyLibrary.TABLE1;
OPEN C2 ;
END
AS400 options for the stored procedure: 存储过程的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 version 7 release 1 AS400版本7发行版1
EDITED: I changed the sql variable as following: 编辑:我更改了sql变量,如下所示:
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.
查询表肯定包含记录。
Look in database for the procudure : StoredProc1. 在数据库中查找该过程:StoredProc1。 It is opening a file that doesn't exist.
它正在打开一个不存在的文件。
To make it works, need to do as following: 要使其工作,需要执行以下操作:
2 different connection options: 2种不同的连接选项:
Using ODBC Connection 使用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];
} }
Using ADODB Connection 使用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 options for the stored procedure: 存储过程的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.