我有一个在ASP.NET和C#中开发的页面。在此特定页面上,登录用户可以查看他们的经理是谁以及经理联系人详细信息等。正在从数据库中检索详细信息。我使用以下代码
protected string ManagerData()
{
string UsrName = User.Identity.Name;
string mName;
string mNum;
using (SqlConnection connection = new SqlConnection(Common.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select ManagerName,ManagerNumber from Managers where UserName=@UserName"))
{
SqlParameter para = new SqlParameter("UserName", UsrName);
cmd.Parameters.Add(para);
cmd.Connection = connection;
connection.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
mName = reader.GetString(0);
mNum = reader.GetString(1);
}
}
}
return //How would i return both of the string
}
返回值。要显示它们,我使用以下代码
<h5>Your Manager is:<%:ManagerData() %> </h5>
但如果我这样做
<h5>Your Managers Number is:<%:ManagerData() %> </h5>
在两种情况下都返回Manager的名称。所以我的问题是我应该编写一个单独的方法来获取管理器编号,还是有办法从上面的方法中返回两个字符串?
提前感谢您的所有帮助和支持
答案 0 :(得分:1)
将参数传递给 ManagerData 功能,无论您需要名称还是数字。
protected string ManagerData(String sValue)
{
string UsrName = User.Identity.Name;
string mName;
string mNum;
using (SqlConnection connection = new SqlConnection(Common.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select ManagerName,ManagerNumber from Managers where UserName=@UserName"))
{
SqlParameter para = new SqlParameter("UserName", UsrName);
cmd.Parameters.Add(para);
cmd.Connection = connection;
connection.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
mName = reader.GetString(0);
mNum = reader.GetString(1);
}
}
}
if(sValue = "name")
return mName;
else
return mNum
}
<h5>Your Manager is:<%:ManagerData("name") %> </h5>
<h5>Your Managers Number is:<%:ManagerData("number") %> </h5>
答案 1 :(得分:0)
您可以使用&#34; out&#34;获取多个值的参数。例如。
Public string mName;
Public string mNum;
protected void ManagerData(out string mName,out string mNum)
{
string UsrName = User.Identity.Name;
string mName;
string mNum;
using (SqlConnection connection = new SqlConnection(Common.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("select ManagerName,ManagerNumber from Managers where UserName=@UserName"))
{
SqlParameter para = new SqlParameter("UserName", UsrName);
cmd.Parameters.Add(para);
cmd.Connection = connection;
connection.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
mName = reader.GetString(0);
mNum = reader.GetString(1);
}
}
}
}
在源页面
<h5>Your Manager is:<%= mName %></h5>
<h5>Your Manager is:<%= mNum %></h5>
执行此函数后,您可以拥有mName和mNum的值。