我想从下面的函数返回多个字符串。怎么做
public static string GETSTATE_SAP(string SAP)
{
string strState = "";
string strSiteAdd = "";
string strLatitude = "";
string strLongtiude = "";
try
{
OracleConnection Oraconn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAPPSAPID"].ConnectionString);
Oraconn.Open();
OracleCommand cmd = new OracleCommand("Select STATE, SITE_NAME, SITE_ADDRESS, latitude, longitude from R4G_OSP.UBR where SAP_ID = '" + SAP + "'", Oraconn);
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataTable dtSap = new DataTable();
da.Fill(dtSap);
strState = Convert.ToString(dtSap.Rows[0][0]);
strSiteAdd = Convert.ToString(dtSap.Rows[0][0]);
strLatitude = Convert.ToString(dtSap.Rows[0][0]);
strLongtiude = Convert.ToString(dtSap.Rows[0][0]);
}
catch (Exception)
{
}
return strState, strSiteAdd, strLatitude, strLongtiude;
}
答案 0 :(得分:4)
有几种方法可以返回信息,我建议你返回一个课程。这是因为信息是相关的,但没有相同的含义。当元素表示相同类型的数据时,可以使用数组或列表。例如,菜单项列表,状态信息列表等。
public class StateInfo
{
public string State {get;set;}
public string SiteAdd {get;set;}
public string Latitude {get;set;}
public string Longtiude {get;set;}
}
public static StateInfo GETSTATE_SAP(string SAP)
{
var result = new StateInfo();
try
{
OracleConnection Oraconn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAPPSAPID"].ConnectionString);
Oraconn.Open();
OracleCommand cmd = new OracleCommand("Select STATE, SITE_NAME, SITE_ADDRESS, latitude, longitude from R4G_OSP.UBR where SAP_ID = '" + SAP + "'", Oraconn);
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataTable dtSap = new DataTable();
da.Fill(dtSap);
result.State = Convert.ToString(dtSap.Rows[0][0]);
result.SiteAdd = Convert.ToString(dtSap.Rows[0][0]);
result.Latitude = Convert.ToString(dtSap.Rows[0][0]);
result.Longtiude = Convert.ToString(dtSap.Rows[0][0]);
}
catch (Exception) // <--- I'll explain below..
{
return null;
}
return result;
}
这样您就不会依赖于返回信息的顺序。这使得处理起来更容易。
用法:
var stateInfo = GETSTATE_SAP(sap);
if(stateInfo != null)
Debug.WriteLine(stateInfo.Longtiude);
尝试/捕获您的方法:
我将你的try / catch标记为'下面的解释'。想象你用这种方法来获取你的信息,你不能“看到”出了什么问题,但信息是空的。被叫无法验证信息是否正确。要么返回null,要么不在此处捕获异常:
答案 1 :(得分:0)
您可以使用这些字段创建自定义类并将其返回,
public MyClass string GETSTATE_SAP(string SAP)
{
MyClass myObj = new MyClass();
try
{
OracleConnection Oraconn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAPPSAPID"].ConnectionString);
Oraconn.Open();
OracleCommand cmd = new OracleCommand("Select STATE, SITE_NAME, SITE_ADDRESS, latitude, longitude from R4G_OSP.UBR where SAP_ID = '" + SAP + "'", Oraconn);
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataTable dtSap = new DataTable();
da.Fill(dtSap);
myObj.strState = Convert.ToString(dtSap.Rows[0][0]);
myObj.strSiteAdd = Convert.ToString(dtSap.Rows[0][0]);
myObj.strLatitude = Convert.ToString(dtSap.Rows[0][0]);
myObj.strLongtiude = Convert.ToString(dtSap.Rows[0][0]);
}
catch (Exception)
{
}
return myObj;
}
public class MyClass
{
public string strState { get; set; }
public string strSiteAdd { get; set; }
public string strLatitude { get; set; }
public string strLongtiude { get; set; }
}
答案 2 :(得分:0)
您的方法返回string
,因此return strState, strSiteAdd, strLatitude, strLongtiude;
语句是非法的。
您可以考虑更改方法签名以返回如下所示的DataTable
并返回您创建的DataTable
public static DataTable GETSTATE_SAP(string SAP)
{
(OR)
更改它以返回字符串数组
public static IEnumerable<string> GETSTATE_SAP(string SAP)
{
答案 3 :(得分:0)
您可以选择以下任何一种
value = count[0];
public class location{
public string strState = "";
public string strSiteAdd = "";
public string strLatitude = "";
public string strLongtiude = "";
}
另一种选择是字典
public static location GETSTATE_SAP(string SAP)
{
_location = new location() ;
try
{
_location.strState = Convert.ToString(dtSap.Rows[0][0]);
_location.strSiteAdd = Convert.ToString(dtSap.Rows[0][0]);
_location.strLatitude = Convert.ToString(dtSap.Rows[0][0]);
_location.strLongtiude = Convert.ToString(dtSap.Rows[0][0]);
}
return _location;
第三个是数据表
public static Dictionary<string,string> GETSTATE_SAP(string SAP) {
Dictionary<string,string> _location = new dictionary<string,string>
_location.add("strState", Convert.ToString(dtSap.Rows[0][0]));`
返回动态对象
datatable _location = new datatable();
_location.add("strState"); // add all columns
_location.rows.add(Convert.ToString(dtSap.Rows[0][0]), , , ,..);
public object MyDynamicObject()
{
dynamic obj = new ExpandoObject();
return obj;
}
答案 4 :(得分:0)
您正在使用Public方法。
我个人不喜欢在公共方法中使用out
参数。
但是如果您不想创建新类,那么您可以尝试以下方法。
public static bool GETSTATE_SAP(string SAP, out string strState, out string strSiteAdd, out string strLatitude, out string strLongtiude)
{
strState = string.Empty;
strSiteAdd = string.Empty;
strLatitude = string.Empty;
strLongtiude = string.Empty;
try
{
OracleConnection Oraconn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionStringAPPSAPID"].ConnectionString);
Oraconn.Open();
OracleCommand cmd = new OracleCommand("Select STATE, SITE_NAME, SITE_ADDRESS, latitude, longitude from R4G_OSP.UBR where SAP_ID = '" + SAP + "'", Oraconn);
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataTable dtSap = new DataTable();
da.Fill(dtSap);
if(dtSap.Rows.Count > 0){
strState = Convert.ToString(dtSap.Rows[0][0]);
strSiteAdd = Convert.ToString(dtSap.Rows[0][0]);
strLatitude = Convert.ToString(dtSap.Rows[0][0]);
strLongtiude = Convert.ToString(dtSap.Rows[0][0]);
return true;
}
}
catch (Exception)
{
}
return false;
}