如何从 ASP.NET 中的 Web 服务返回条件列表?
我想通过 Web 服务返回一个列表,该服务采用一个字符串来更新提供该列表的 SQL。我对构建列表不太熟悉,但我的课堂上已经有了一个列表。再次,我想通过网络服务返回此列表。
我的表类中的当前列表:
static public List<Skills> GetSkillsList(string like)
{
List<Skills> thelist = new List<Skills>();
string sql = "SELECT * FROM Skills WHERE SkillName LIKE '%" + like + "%'";
SqlDataReader dr = DBUtil.FillDataReader(sql);
while (dr.Read())
{
Skills obj = new Skills();
obj.skillID = Convert.ToInt32(dr["skillID"].ToString());
obj.skillName = Convert.ToString(dr["skillName"].ToString());
obj.skillReviewed = Convert.ToBoolean(dr["skillReviewed"].ToString());
obj.skillActive = Convert.ToBoolean(dr["skillActive"].ToString());
thelist.Add(obj);
}
return thelist;
}
Web 服务中的虚拟列表:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
[WebMethod]
public IList<string> GetSkills(string contains)
{
IList<string> output = new List<string>();
output.Add("Apple");
output.Add("Ajax");
output.Add("Alpha");
output.Add("Alphred");
return output;
}
}
我想返回 Web 服务列表中的名称和 ID 字段。
我认为它会是这样的:
[WebMethod]
public IList<string> SkillsList(string like)
{
IList<string> output = new List<string>();
output = Skills.GetSkillsList(like);
return output;
}
但我知道这是不正确的。
任何帮助表示赞赏!
I'd like to return a list through a web service that takes a string which updates the SQL that supplies the list. I'm not too familiar with building lists, but I already have a list in my class. Again, I'd like to return this list through the web service.
My current list in my table class:
static public List<Skills> GetSkillsList(string like)
{
List<Skills> thelist = new List<Skills>();
string sql = "SELECT * FROM Skills WHERE SkillName LIKE '%" + like + "%'";
SqlDataReader dr = DBUtil.FillDataReader(sql);
while (dr.Read())
{
Skills obj = new Skills();
obj.skillID = Convert.ToInt32(dr["skillID"].ToString());
obj.skillName = Convert.ToString(dr["skillName"].ToString());
obj.skillReviewed = Convert.ToBoolean(dr["skillReviewed"].ToString());
obj.skillActive = Convert.ToBoolean(dr["skillActive"].ToString());
thelist.Add(obj);
}
return thelist;
}
Dummy list in the web service:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
[WebMethod]
public IList<string> GetSkills(string contains)
{
IList<string> output = new List<string>();
output.Add("Apple");
output.Add("Ajax");
output.Add("Alpha");
output.Add("Alphred");
return output;
}
}
I'd like to return the Name and the ID fields in the web service list.
I would think it would be something like:
[WebMethod]
public IList<string> SkillsList(string like)
{
IList<string> output = new List<string>();
output = Skills.GetSkillsList(like);
return output;
}
but I know this is incorrect.
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是如何在您提供的 WebMethod 中执行此操作。正如所建议的,最好使用一个新类来保存 ID 和名称。如果您需要使用 List,则可以使用分隔字符串。
Here's how to do it in the WebMethod you provided. As suggested, a new class to hold the ID and Name would be best. Although you could do a delimited string if you have a requirement to use List.
我会创建另一个仅包含 ID 和 Name 属性的类,填充该类的列表并将其返回给调用者。我创建了一个示例控制台应用程序。请参阅下面的代码,如果有帮助请告诉我。
I would have created another class with only ID and Name property, populate the list of that class and return it to the caller. I have created a sample console app. See the code below and let me know if it helps.