10,调用SQL存储过程
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; //注意
using System.Data.OracleClient; //Oracle数据库
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
OracleConnection thisConnection = new OracleConnection(@"Data Source=orcl;User ID=system;Password=myoracle;Unicode=True");
thisConnection.Open();//此处可以不用打开
OracleCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandType = CommandType.StoredProcedure;
thisCommand.CommandText = "or_test";
int rows = 0;
string rtnStr;
try
{
OracleParameter[] parm = new OracleParameter[2];
//in
parm[0] = new OracleParameter("myid", OracleType.Number, 3);
parm[1] = new OracleParameter("myname", OracleType.VarChar, 50);
//指明参数是输入还是输出型
parm[0].Direction = ParameterDirection.Input;
parm[1].Direction = ParameterDirection.Output;
//给参数赋值
parm[0].Value = 350;
//传递参数给Oracle命令
thisCommand.Parameters.Add(parm[0]);
thisCommand.Parameters.Add(parm[1]);
rows = thisCommand.ExecuteNonQuery();//返回行数
//取出返回值
rtnStr = Convert.ToString(parm[1].Value);//res
Console.WriteLine(rtnStr);
Console.WriteLine(Convert.ToString(rows));
}
catch (Exception er)
{
string merr = "err:" + er.ToString();
Console.WriteLine(merr);
}
finally
{
thisConnection.Close();
thisConnection.Dispose();//释放所有资源
thisCommand.Parameters.Clear();
thisCommand.Dispose();//释放所有资源
}
}
}
}
存储过程: