昨天使用 Data Block 操作 oracle 返回 cursor 期间产生了一点问题很是郁闷找了一下午也没有解决早上睡不着起来继续找结果找到了解决的方法其实也是怪自己没有很好的看文档在此记录一下以使别的同志再出现我的问题的时候很容易的找到解决的方法
问题是这样的
我在oracle里面有这样一个过程
PROCEDURE ListAllStatic_Users (cur_Static_User OUT T_CURSOR)
IS
BEGIN
OPEN cur_Static_User FOR
SELECT * FROM Static_User ;
END ListAllStatic_Users;
我在程序里面如下调用
Database db = DatabaseFactoryCreateDatabase(oraserver);
string sqlCommand = Static_UserPackageListAllStatic_Users;
DBCommandWrapper dbCommandWrapper =dbGetStoredProcCommandWrapper(sqlCommand);
DataSet dsCustomers = dbExecuteDataSet(dbCommandWrapper);
DataGridDataSource=dsCstomers;
DataGridDataBind();
结果出现如下问题
ORA: 第 行 第 列: PLS: 调用 LISTALLSTATIC_USERS 时参数个数或类型错误 ORA: 第 行 第 列: PL/SQL: Statement ignored
说明: 执行当前 Web 请求期间出现未处理的异常请检查堆栈跟蹤信息以了解有关该错误以及代码中导致错误的出处的详细信息
异常详细信息: SystemDataOracleClientOracleException: ORA: 第 行 第 列: PLS: 调用 LISTALLSTATIC_USERS 时参数个数或类型错误 ORA: 第 行 第 列: PL/SQL: Statement ignored
源错误:
行
行 DataSet dsCustomers = dbExecuteDataSet(dbCommandWrapper);行 DataGridDataSource=dsCustomers;
行 DataGridDataBind();
我以为是我的参数没有弄对于是就加了一句
dbCommandWrapperAddOutParameter(cur_Static_UserDbTypeObject);
结果还是一样的后来也试验了
OracleCommandWrapperAddParameter(stringDbTypeintParameterDirectionboolbytebytestringDataRowVersionobject);
这个方法来添加也是不行
后来就上网找了很长时间也没有什么进展今天早上起来还是一筹莫展偶尔的打开Enterprise Library安装目录的Enterprise Library Release Notesrtf文件发现里面有这么一段
Data Access Application Block: Default Oracle cursor cur_OUT
The managed provider for Oracle requires you to explicitly bind your reference cursor in your parameter collection This means you must explicitly create an output parameter for the cursor in your application code However that code will not be portable with database systems that do not require a parameter for the cursor The OracleDatabase allows you to create commands without specifying a cursor It will create a cursor named cur_OUT for commands that execute a stored procedure and do not include an output parameter for the cursor This means that you can name your reference cursor as cur_OUT and the Data Access Application Block will bind it for you; you do not need to explicitly create an output parameter for the cursor If your stored procedures use a cursor with a name other than cur_OUT you must explicitly add a parameter for each cursor to the command Similarly if your stored procedure contains multiple cursors you must explicitly add each cursor parameter to the command
这下我就明白了在我的oracle函数中我的名字 cur_Static_User 和默认的名字cur_OUT不匹配
于是我就改了我的存储过程的参数名称cur_Static_User改为 cur_OUT
问题就解决了
经过试验也可以用如下方法用自己的参数名而不用默认的参数名也可以在一个PROCEDURE中返回多个 CURSOR
我的存储过程
Procedure STATIC_USER_SelectAll
( cur_OUT_f OUT T_OUT cur_OUT_g OUT T_OUT)
AS
Begin
OPEN cur_OUT_f FOR Select * from STATIC_USER;
OPEN cur_OUT_g FOR Select * from STATIC_ROLE;
End;
Procedure STATIC_USER_SelectAll
( cur_OUT_f OUT T_OUT cur_OUT_g OUT T_OUT)
AS
Begin
OPEN cur_OUT_f FOR Select * from STATIC_USER;
OPEN cur_OUT_g FOR Select * from STATIC_ROLE;
End;
Procedure STATIC_USER_SelectAll
( cur_OUT_f OUT T_OUT cur_OUT_g OUT T_OUT)
AS
Begin
OPEN cur_OUT_f FOR Select * from STATIC_USER;
OPEN cur_OUT_g FOR Select * from STATIC_ROLE;
End; 代码如下
Database db = DatabaseFactoryCreateDatabase(oraserver);
string sqlCommand = Static_UserPackageSTATIC_USER_SelectAll;
MicrosoftPracticesEnterpriseLibraryDataOracleOracleCommandWrapper dbCommandWrapper =(MicrosoftPracticesEnterpriseLibraryDataOracleOracleCommandWrapper)dbGetStoredProcCommandWrapper(sqlCommand);
dbCommandWrapperAddParameter(cur_OUT_f OracleTypeCursor ParameterDirectionOutput true StringEmpty DataRowVersionDefault ConvertDBNull);
dbCommandWrapperAddParameter(cur_OUT_g OracleTypeCursor ParameterDirectionOutput true StringEmpty DataRowVersionDefault ConvertDBNull);
DataSet dsCustomers = dbExecuteDataSet(dbCommandWrapper);
DataGridDataSource=dsCustomersTables[];
DataGridDataBind();
DataGridDataSource=dsCustomersTables[];
DataGridDataBind();