C#調用Oracle存儲過程


1.創建表

create table test
(ID number,
NAME varchar2(10),
SEX varchar2(4),
AGE number,
ADDRESS varchar2(200)
);

2.創建不帶參數的存儲過程

create or replace procedure proc1
is
begin insert into test(ID,NAME,SEX,AGE) values
(1,'moses','man',25);
commit;
end;
/

3.寫C#代碼調用這個不帶參數的存儲過程

protected void Button2_Click(object sender, EventArgs e)
    {
        String oc = ConfigurationManager.ConnectionStrings["conn"].ToString();
        OracleConnection conn = new OracleConnection(oc);
        conn.Open();
        OracleCommand orm = conn.CreateCommand();
        orm.CommandType = CommandType.StoredProcedure;
        orm.CommandText = "proc1";
        orm.ExecuteNonQuery();
        conn.Close();
    }

4.寫一個沒有返回值的帶參數的存儲過程

create or replace proc2
(v_id  number,
v_name varchar2
)
is begin insert into test(id,name)
values(v_id,v_name);
commit;
end;
/

5.C#調用這個帶參數無返回值的存儲過程

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.TextBox2.Text))
        {
            this.TextBox2.Text = "編號不能為空";
            this.TextBox2.Focus();
            return;

        }
        if (string.IsNullOrEmpty(this.TextBox3.Text))
        {
            this.TextBox3.Text = "姓名不能為空";
            this.TextBox3.Focus();
            return;
           

        }
       String or=ConfigurationManager.ConnectionStrings["conn"].ToString();
       OracleConnection oc = new OracleConnection(or);
       oc.Open();
       OracleCommand om = oc.CreateCommand();
       om.CommandType = CommandType.StoredProcedure;
       om.CommandText = "proc2";
       om.Parameters.Add("v_id", OracleType.Number).Direction = ParameterDirection.Input;
       om.Parameters["v_id"].Value = this.TextBox2.Text.Trim();
       om.Parameters.Add("v_name", OracleType.NVarChar).Direction = ParameterDirection.Input;
       om.Parameters["v_name"].Value = this.TextBox3.Text.Trim();
       om.ExecuteNonQuery();
       oc.Close();
    }

6.寫一個帶參數有返回值的存儲過程

 

create or replace procedure proc3 (recount out number
)
is 
begin
select  count(*)  into reccount from test;
commit;
end;
/

7.C#調用這個帶參數有返回值的存儲過程

protected void Button1_Click(object sender, EventArgs e)
    {
        String or = ConfigurationManager.ConnectionStrings["conn"].ToString();
        OracleConnection oc = new OracleConnection(or);
        oc.Open();
        OracleCommand ocm = oc.CreateCommand();
        ocm.CommandType = CommandType.StoredProcedure;
        ocm.CommandText = "proc3";
        ocm.Parameters.Add("reccount", OracleType.Number).Direction = ParameterDirection.Output;
        ocm.ExecuteNonQuery();
        this.TextBox1.Text = ocm.Parameters["reccount"].Value.ToString();


       
    }

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM