gpt4 book ai didi

c# - 从 asmx 服务返回多行

转载 作者:太空狗 更新时间:2023-10-29 22:14:57 24 4
gpt4 key购买 nike

我有一个 Web 服务方法,我想从数据表中返回多行。

我熟悉网络服务方法的返回值,但不熟悉数据表中的多行。执行此操作的最佳方法是什么?我需要返回一个数组还是 list<>

我的代码方法是这样设置的。

[WebMethod]
public void UpdateBold(int count, float lat, float lng)
{
DataTable dt = new Gallery().DisplayNearestByLatLong(count, lat, lng);

// return code here
}

最佳答案

您可以为数据表项创建新类型并返回该数据的数组

    public class sample
{
public string val1;
public string val2;

}
[WebMethod]
public sample[] UpdateBold(int count, float lat, float lng)

{

DataTable dt = new Gallery().DisplayNearestByLatLong(count, lat, lng);
var samples = new List<sample>();

foreach(DataRow item in dt.Rows)
{
var s = new sample();
s.val1 = item[0].ToString();
s.val2 = item[1].ToString();
samples.Add(s);
}
return samples.ToArray();
}

对于 Ajax:

消费见http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/但是你应该让你的网络服务序列化 JSON,为此请参见 http://msdn.microsoft.com/en-us/library/bb763183.aspx

关于c# - 从 asmx 服务返回多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4359934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com