C # calling the Oracle Stored Procedure

Source: Internet
Author: User
Tags oracleconnection

This article mainly describes how to use C # To Call The Oracle stored procedures, as well as the returned results, related result sets, and functions. First, we will briefly describe how to create a PACKAGE and package body in Oracle, the function and stored procedure return result set are defined here.

1: Create a PACKAGE:

 
 
  1. CREATE OR REPLACE package SCOTT.pk_wt  
  2. is  
  3. type mytype is ref cursor;  
  4. procedure p_wt(mycs out mytype);  
  5. function f_get(str in varchar2)  
  6. return varchar2;  
  7. end;   

Note: PACKAGE is just a declaration. Here we define an Oracle stored procedure to return a collection and a function to return a string.

2: Create a package body:

 
 
  1. CREATE OR REPLACE package BODY SCOTT.pk_wt  
  2. is  
  3. procedure p_wt(mycs out mytype)  
  4. is  
  5. begin  
  6. open mycs for select * from test;  
  7. end p_wt;  
  8. function f_get(str varchar2)  
  9. return varchar2  
  10. is  
  11. str_temp varchar2(100) := 'good luck!';   
  12. begin  
  13. str_temp := str_temp || str;  
  14. return str_temp;  
  15. end f_get;  
  16. end pk_wt;  
  17. /  

Note: the creation of the package body here is a specific description and use, and how will the package body be implemented.

C # section:

In C #, the code is divided into two parts: Using Functions and using result sets.

Define a connection and get it from WEBCONFIG:

 
 
  1. private OracleConnection orcn=new 
    OracleConnection(System.Configuration.
    ConfigurationSettings.AppSettings["scott"]); 

C # Call Oracle functions:

 
 
  1. OracleCommand cmd=new OracleCommand("pk_wt.f_get",orcn);  
  2. cmd.CommandType=CommandType.StoredProcedure;  
  3. OracleParameter p1=new OracleParameter("str",OracleType.VarChar,10);  
  4. p1.Direction=System.Data.ParameterDirection.Input;  
  5. p1.Value=this.TextBox1.Text;  
  6. OracleParameter p2=new OracleParameter("result",OracleType.VarChar,100);  
  7. p2.Direction=System.Data.ParameterDirection.ReturnValue;  
  8. cmd.Parameters.Add(p1);  
  9. cmd.Parameters.Add(p2);  
  10. orcn.Open();  
  11. cmd.ExecuteNonQuery();  
  12. orcn.Close();  
  13. this.Button_function.Text=p2.Value.ToString(); 

The RESULT is the return variable of the system-defined function. Note that the return type of the function parameter must be specified, and the return type of the COMMAND type must also be specified, in addition, there is no difference with General Oracle stored procedures.

C # Call the Oracle return result set:
 

 
 
  1. OracleCommand cmd=new OracleCommand("pk_wt.p_wt",orcn);  
  2. cmd.CommandType=CommandType.StoredProcedure;  
  3. OracleParameter p1=new OracleParameter("mycs",OracleType.Cursor);  
  4. p1.Direction=System.Data.ParameterDirection.Output;  
  5. cmd.Parameters.Add(p1);  
  6. OracleDataAdapter da=new OracleDataAdapter(cmd);  
  7. DataSet ds=new DataSet();  
  8. da.Fill(ds,"test");  
  9. this.DataGrid1.DataSource=ds;  
  10. this.DataGrid1.DataBind(); 

The above content is the description of C # calling the Oracle stored procedure, hoping to help you in this regard.

Related Article

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.