Solve the problem that dataset is returned when the stored procedure is executed in Oracle in C #.

Source: Internet
Author: User

Solution 1

After the stored procedure is executed, dataset is returned:

1. There must be an output parameter cursor in the stored procedure to return the Stored Procedure

 
 
  1. -- Create a temporary table returned by the Stored Procedure
  2. Create global temporary table tmp_his_pptn_jp
  3. (
  4. Stcd varchar2 (12) NotNull,
  5. Stnm varchar2 (50 ),
  6. Addvcd varchar2 (6 ),
  7. Rgnnm varchar2 (50 ),
  8. Hisavg number (13, 3 ),
  9. ACCP number (10, 1 ),
  10. JP number (10, 2)
  11. )
  12. On commit Delete rows; -- [1] delete data after the transaction is completed
  13. Alter table tmp_his_pptn_jp
  14. Add primary key (stcd );
  15. -- Create a stored procedure
  16. Create or replace procedure proc_rain_jp (
  17. V_stcds varchar2, -- requires v_ptm1, v_ptm2 not to crash into years, and returns the temporary table tmp_his_pptn_jp
  18. V_ptm1 varchar2,
  19. V_ptm2 varchar2,
  20. V_cs out sys_refcursor
  21. )
  22. As
  23. -- Define variables ......
  24. Begin
  25. -- Data processing ......
  26. Open v_cs for select * From tmp_his_pptn_jp;
  27. Return;
  28. End;

2. Run the stored procedure in C # using Oracle

 
 
  1. Idbconnection con =This. Dbinterface. createconnection ();// Custom data access interface 
  2. Con. open ();
  3.  
  4. Idbtransaction trans = con. begintransaction ();
  5. Cmd = con. createcommand ();
  6. Cmd. Transaction = trans;// SET transaction for command 
  7.  
  8. Cmd. commandtype = system. Data. commandtype. storedprocedure;
  9. Cmd. commandtext ="Proc_rain_jp";
  10. System. Data. oracleclient. oracleparameter P;
  11. P =NewSystem. Data. oracleclient. oracleparameter ("V_stcds", System. Data. oracleclient. oracletype. varchar, 2000 );
  12. Cmd. Parameters. Add (P );
  13. P. Direction = system. Data. parameterdirection. input;
  14. P. value = STC;
  15.  
  16. P =NewSystem. Data. oracleclient. oracleparameter ("V_ptm1", System. Data. oracleclient. oracletype. varchar, 20 );
  17. Cmd. Parameters. Add (P );
  18. P. Direction = system. Data. parameterdirection. input;
  19. P. value =This. Getparamvalue ("Sdate");
  20.  
  21. P =NewSystem. Data. oracleclient. oracleparameter ("V_ptm2", System. Data. oracleclient. oracletype. varchar, 20 );
  22. Cmd. Parameters. Add (P );
  23. P. Direction = system. Data. parameterdirection. input;
  24. P. value =This. Getparamvalue ("Edate");
  25.  
  26.  
  27. // Output Dataset 
  28. P =NewSystem. Data. oracleclient. oracleparameter ("V_cs", System. Data. oracleclient. oracletype. cursor );
  29. Cmd. Parameters. Add (P );
  30. P. Direction = system. Data. parameterdirection. output;// Set it to output 
  31.  
  32. Dataset DS =NewDataset ();
  33. Idbdataadapter da =NewSystem. Data. oracleclient. oracledataadapter (CMDAsSystem. Data. oracleclient. oraclecommand );
  34. Da. Fill (DS );
  35.  
  36. Trans. Commit ();
  37. If(Con. State! = Connectionstate. Closed)
  38. ...{
  39. Try 
  40. ...{
  41. Con. Close ();
  42. }
  43. Catch 
  44. ...{
  45. }
  46. }
  47. ReturnDS. Tables [0];
  48.  

3. Pay attention to the above C #CodeWe open a transaction. Why:

If we do not use transactions, there is no problem in sqlplus debugging, but in yes. net execution time will be reported ORA-08103: Object no longer exists error, the reason is in the stored procedure, the temporary table creation option is changed from on commit Delete rows [1] to on commit preserve rows. when querying temporary table data on the. NET page, more duplicate records are required for each query. The reason is that the session connection of Oracle is not completed. Therefore, records must be inserted before each execution of the stored procedure. Why is Oracle session not over? It must be an ASP. NET service.ProgramThe connection to the database is maintained in the data connection pool. However, for the sake of performance, we cannot use the connection pool. In this way, temporary tables based on Oracle sessions cannot be used.

Return to the temporary table based on Oracle transactions, that is, use on commit Delete rows to create the temporary table. Then, the ODP transaction processing mechanism is called in the ASP. NET application, so that the problem of returning dataset through Oracle execution in C # can be solved!

Note:

(1) theoretically, do not execute commit in the stored procedure, that is, do not use PL/SQL transaction processing in the stored procedure; otherwise, Asp. NET page cannot obtain data, because after commit, the data in the temporary table is automatically cleared.

(2) theoretically, the Oracle transaction processing mechanism provided by oledb or Microsoft should also work without ODP.

 

Solution 2

-- Baotou
Create or replace package pkg_test
As
Type mycursor is ref cursor;
Function get (p_id number) return mycursor;
End pkg_test;


-- Package body
Create or replace package body pkg_test
As
-- Input ID to return the record set function
Function get (p_id number) return mycursor is
RC mycursor;
Strsql varchar2 (200 );
Begin
If p_id = 0 then
Open RC for select a. user_name from fnd_user;
Else
Strsql: = 'select A. user_name from fnd_user A where a. user_id =: p_id ';
Open RC for strsql using p_id;
End if;
Return RC;
End get;

End pkg_test;

-- Call Test
Set serverout on
Declare
W_rc pkg_test.mycursor;
W_name varchar2 (100 );
Begin
W_rc: = pkg_test.get (0 );
Loop
Fetch w_rc into w_name;
Exit when w_rc % notfound;
Dbms_output.put_line (w_name );
End loop;
End;
/

 

We can see that the second scheme adds a cursor Declaration (type mycursor is ref cursor) in the header definition. In fact, in the first scheme, we use "sys_refcursor ", this is what the system has defined for us. Therefore, the second solution seems a bit redundant.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.