本文介绍了从方法返回多个值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从这个c#方法返回2个输出参数,并返回第二个ruturn参数的错误。



我尝试过:



public string getEMails(string staffid)

{

using(OracleConnection OracleConn = new OracleConnection) (或))

{

OracleConn.Open();

尝试

{

query =SELECT uetc.team_codes,LPAD(papf.employee_number,5,0)staff_id,papf.full_name full_name AND papf.employee_number = @employee_number;

OracleCommand OraC = new OracleCommand(查询,OracleConn);

OraC.CommandType = CommandType.Text;

OraC.Parameters.Add(新的OracleParameter(employee_number,OracleDbType.Varchar2))。Value = staffid;

//OraC.Parameters.Add(new OracleParameter(SESSION_ID,OracleDbTy) pe.Varchar2))。Value = SessionID;

//OraC.Parameters.Add(new OracleParameter(V_RESULT,OracleDbType.Varchar2,32767))。Direction = ParameterDirection.Output;





// OracleDataReader dr = OraC.ExecuteNonQuery();

OracleDataReader dr = OraC.ExecuteReader();

while(dr.Read())

{

SupervisorEmail = dr [EMAIL_ADDRESS_1]。ToString();

StaffEmail = dr [EMAIL_ADDRESS]。ToString();

string id = dr [STAFF_ID]。ToString();



}



}

catch(例外情况)

{



ex.Message.ToString();

}

终于

{

OracleConn.Close();

}

返回SupervisorEmail;

返回StaffEmail;



}

}

im trying to return 2 output parameters from this c# method and it returning an error for the second ruturn parameter.

What I have tried:

public string getEMails(string staffid)
{
using (OracleConnection OracleConn = new OracleConnection(or))
{
OracleConn.Open();
try
{
query = "SELECT uetc.team_codes, LPAD (papf.employee_number, 5, 0) staff_id,papf.full_name full_name AND papf.employee_number = @employee_number";
OracleCommand OraC = new OracleCommand(query, OracleConn);
OraC.CommandType = CommandType.Text;
OraC.Parameters.Add(new OracleParameter("employee_number", OracleDbType.Varchar2)).Value = staffid;
//OraC.Parameters.Add(new OracleParameter("SESSION_ID", OracleDbType.Varchar2)).Value = SessionID;
//OraC.Parameters.Add(new OracleParameter("V_RESULT", OracleDbType.Varchar2, 32767)).Direction = ParameterDirection.Output;


//OracleDataReader dr = OraC.ExecuteNonQuery();
OracleDataReader dr = OraC.ExecuteReader();
while (dr.Read())
{
SupervisorEmail = dr["EMAIL_ADDRESS_1"].ToString();
StaffEmail = dr["EMAIL_ADDRESS"].ToString();
string id = dr["STAFF_ID"].ToString();

}

}
catch (Exception ex)
{

ex.Message.ToString();
}
finally
{
OracleConn.Close();
}
return SupervisorEmail;
return StaffEmail;

}
}

推荐答案

public string getEMails(string staffid, out staffEmail)
   {
   ...
   staffEmail = ... ;
   return SupervisorEmail;
   }




public string getEMails(string staffid, ref staffEmail)
   {
   ...
   staffEmail = ... ;
   return SupervisorEmail;
   }

并调用它将适当的变量传递给方法:

And call it passing the appropriate variable to the method:

string staffEmail;
string supEmail = getEMails("00001", out staffEmail);




string staffEmail = "none";
string supEmail = getEMails("00001", ref staffEmail);





或者,你可以创建一个容器类,它包含您的参数,并返回:



Or, you could create a "container class" which holds both your parameters, and return that:

public class EMails
   {
   string Staff {get; set}
   string Supervisor {get; set;}
   }
...
public EMails getEMails(string staffid)
   {
   ...
   Emails em = new Emails;
   em.Staff = StaffEmail;
   em.Supervisor = SupervisorEmail;
   return em;
   }


这篇关于从方法返回多个值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 23:17