如何从 ASP.NET 中的 Web 服务返回条件列表?

发布于 2024-12-11 20:22:04 字数 1770 浏览 0 评论 0原文

我想通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2024-12-18 20:22:04

以下是如何在您提供的 WebMethod 中执行此操作。正如所建议的,最好使用一个新类来保存 ID 和名称。如果您需要使用 List,则可以使用分隔字符串。

public class Pair
{
     public string ID { get; set; }
     public string Name { get; set; }
}

[WebMethod]
public IList<Pair> SkillsList(string like)
{
    IList<Skills> mySkills = new List<Skills>();
    IList<Pair> output = new List<Pair>();
    mySkills = Skills.GetSkillsList(like);

    foreach(Skills currentSkill in mySkills)
    {
        Pair p = new Pair();
        p.ID = currentSkill.ID;
        p.Name = currentSkill.Name;

        output.Add(p);
    }

    return output;
}

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.

public class Pair
{
     public string ID { get; set; }
     public string Name { get; set; }
}

[WebMethod]
public IList<Pair> SkillsList(string like)
{
    IList<Skills> mySkills = new List<Skills>();
    IList<Pair> output = new List<Pair>();
    mySkills = Skills.GetSkillsList(like);

    foreach(Skills currentSkill in mySkills)
    {
        Pair p = new Pair();
        p.ID = currentSkill.ID;
        p.Name = currentSkill.Name;

        output.Add(p);
    }

    return output;
}
掩饰不了的爱 2024-12-18 20:22:04

我会创建另一个仅包含 ID 和 Name 属性的类,填充该类的列表并将其返回给调用者。我创建了一个示例控制台应用程序。请参阅下面的代码,如果有帮助请告诉我。

public class Test {
        public int A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
    }

    public class TestR {
        public int A { get; set; }
        public string B { get; set; }
    }

    class Program
    {

        static void Main(string[] args)
        {
            List<Test> list = new List<Test>();
            list.Add(new Test { A=1, B="AAAAA", C="BBBBB"});
            list.Add(new Test { A = 2, B = "BBBBB", C = "CCCCC" });
            list.Add(new Test { A = 3, B = "CCCCC", C = "DDDDD" });

            List<TestR> r = new List<TestR>();
            list.ForEach(l=> r.Add(new TestR { A= l.A, B=l.B }));
            Console.ReadLine();
        }
    }

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.

public class Test {
        public int A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
    }

    public class TestR {
        public int A { get; set; }
        public string B { get; set; }
    }

    class Program
    {

        static void Main(string[] args)
        {
            List<Test> list = new List<Test>();
            list.Add(new Test { A=1, B="AAAAA", C="BBBBB"});
            list.Add(new Test { A = 2, B = "BBBBB", C = "CCCCC" });
            list.Add(new Test { A = 3, B = "CCCCC", C = "DDDDD" });

            List<TestR> r = new List<TestR>();
            list.ForEach(l=> r.Add(new TestR { A= l.A, B=l.B }));
            Console.ReadLine();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文