標籤:
原文:
http://tech.it168.com/oldarticle/2006-04-02/200604021512359.shtml
http://www.cnblogs.com/m-cnblogs/archive/2012/02/23/2364906.html
1.建立測試預存程序:
SQL> create or replace procedure proc_test(p1 IN varchar2,p2 OUT varchar2) is
2 begin
3 SELECT p1 INTO p2 FROM dual;
4 end proc_test;
5 /
過程被建立
2.主要C# 代碼以及注意點:
using ORAC = System.Data.OracleClient;
private void button1_Click(object sender, System.EventArgs e)
{
try
{
string str_Sql = @"call proc_test(:p1,:p2)";
/*不能用:call proc_test(?,?)或者call proc_test(@p1,@p2),這樣會報ORA-01036:非法的變數名/編號錯誤 */
ORAC.OracleCommand cmd = new ORAC.OracleCommand(str_Sql,this.oracleConnection1);
/*cmd.CommandType = CommandType.StoredProcedure;-注意這種方式調用預存程序,不能指定CommandType為StoredProcedure */
ORAC.OracleParameter pram1 = new ORAC.OracleParameter("p1",ORAC.OracleType.VarChar,10);
pram1.Value = "test";
cmd.Parameters.Add(pram1);
ORAC.OracleParameter pram2 = new ORAC.OracleParameter("p2",ORAC.OracleType.VarChar,10);
pram2.Direction =ParameterDirection.Output;
cmd.Parameters.Add(pram2);
if(this.oracleConnection1.State == System.Data.ConnectionState.Closed)
{
this.oracleConnection1.Open();
}
cmd.ExecuteNonQuery();
this.textBox1.Text = cmd.Parameters[1].Value.ToString();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.oracleConnection1.Close();
}
}
參數說明
parameter.Value=賦值
當這個值為null時,不能直接賦值,負責頁面就會報錯:未將對象應用到對象的執行個體
當為null時需轉化為System.DBNull.Value即可
調用Oracle預存程序並擷取out參數值