ORA-06550,调用Oracle存储过程的参数数目或参数类型错误

时间:2012-12-04 17:32:50

标签: ado.net oracle10g odp.net

我有一个存储过程,其中一个参数和一个out参数如下:

create or replace procedure worker_name (w_id in number, w_first out worker.first_name%type) is

Begin
  select first_name into w_first
  from worker
  where worker_id = w_id;
end;

调用此存储过程的代码:

public DataTable <b>GetEmployeeName</b>(int _employeeID)
{
            ArrayList arrEmployeeName = new ArrayList();
            OracleParameter paramEmployeeId = new OracleParameter(":employeeid", _employeeID);
            arrEmployeeName.Add(paramEmployeeId);
            DataLayer obj = new DataLayer();
            DataTable tblEmployee = obj.<b>GetData</b>("macw_conv.worker_name", arrEmployeeName, "SP");

            if (tblEmployee.Rows.Count &gt; 0)
            {
                return tblEmployee;
            }
            return null;
        }

public DataTable <b>GetData</b>(string query, ArrayList parameters,string queryType)
{
            //DataTable dt = new DataTable();
            try
            {
                _con =
                    new OracleConnection(Oradb);
                _con.Open();
                _cmd = new OracleCommand(query, _con);
                if (_cmd.Connection.State == ConnectionState.Open)
                {
                    if (queryType == "SP" &amp;&amp; parameters != null)
                    {
                        _cmd.CommandType = CommandType.StoredProcedure;
                        if (parameters.Count &gt; 0)
                        {
                            foreach (OracleParameter param in parameters)
                            {
                                _cmd.Parameters.Add(param);
                            }
                        }
                    }
                }
                DataSet ds = new DataSet();
                OracleDataAdapter da = new OracleDataAdapter(_cmd);
                da.Fill(ds);
                if (ds.Tables.Count &gt; 0)
                {
                    return ds.Tables[0];
                }
                return null;
            }

我想我无法看到明显的错误。这与存储过程中的out参数和代码中的输出参数之间的数据类型不匹配有关。任何形式的帮助都表示赞赏。

谢谢!

我想出了问题:这是因为保存out参数的变量的数据类型未得到正确处理。我更改了代码如下:

try
{
            _con = new OracleConnection(Oradb);
            _cmd = _con.CreateCommand();
            _cmd.CommandType = CommandType.StoredProcedure;
            _cmd.CommandText = "macw_conv.worker_name";

            OracleParameter inobj1 = _cmd.Parameters.Add("w_id", OracleDbType.Int32,50);
            inobj1.Direction = ParameterDirection.Input;
            inobj1.Value = _employeeID;

            OracleParameter inobj2 = _cmd.Parameters.Add("w_last", OracleDbType.Int32, 50);
            inobj2.Direction = ParameterDirection.Input;
            inobj2.Value = String.IsNullOrEmpty(_lastName) ? null : _lastName;


            OracleParameter outobj = _cmd.Parameters.Add("w_first", OracleDbType.Varchar2, 50);
            outobj.Direction = ParameterDirection.Output;
            _con.Open();
            _cmd.ExecuteNonQuery();
            _employeeName = ((OracleString) _cmd.Parameters[1].Value).ToString();
            _cmd.Dispose();
            _con.Close();
}
catch (OracleException ex)
{
    Console.WriteLine(ex.Message);
}

return _employeeName;  

1 个答案:

答案 0 :(得分:0)

尝试

public DataTable GetEmployeeName(int _employeeID)
        {
            ArrayList arrEmployeeName = new ArrayList();

            OracleParameter paramEmployeeId = new OracleParameter("employeeid", _employeeID);
            arrEmployeeName.Add(paramEmployeeId);

            OracleParameter paramEmployeeFirst = new OracleParameter("first", OracleDbType.Varchar2, ParameterDirection.Output);
            arrEmployeeName.Add(paramEmployeeFirst);

            DataLayer obj = new DataLayer();
            DataTable tblEmployee = obj.GetData("macw_conv.worker_name(:1,:2)", arrEmployeeName, "SP");
            if (tblEmployee.Rows.Count &gt; 0)
            {
                return tblEmployee;
            }
            return null;
        }