0

我有一个存储过程,它有一个输入参数和一个输出参数,如下所示:

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;  
4

1 回答 1

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;
        }
于 2012-12-05T08:25:41.990 回答