如何返回一个对象数组

    数据库有一个Person表,那么,我们做一个person实体。在webservice里面写一个发布方法,发布一个查询方法,可能查询结果有很多个Person,所以应该返回一个数组,问题在于,数组不定长,所以应该使用ArrayList做一下中转,之后调用toArray()方法变成Person[],即return (Person[])result.toArray(typeof(person)).

    代码如下:

    Person.cs

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

/// <summary>
/// Person 的摘要说明
/// </summary>

public   class  Person
{
    
private int _personID;
    
public int PersonID
    
{
        
get
        
{
            
return this._personID;
        }

        
set
        
{
            
this._personID = value;
        }

    }

    
private string _name;
    
public string Name
    
{
        
get
        
{
            
return this._name;
        }

        
set
        
{
            
this._name = value;
        }

    }

    
private string _birthday;
    
public string Birthday
    
{
        
get
        
{
            
return this._birthday;
        }

        
set
        
{
            
this._birthday = value;
        }

    }


    
public Person()
    
{
    }



    
public Person(string pname,string pbirthday)
    
{
        
this._name = pname;
        
this._birthday = pbirthday;
    }


    
public Person(DataRow record)
    
{
        
this._personID = Convert.ToInt16(record["personID"]);
        
this._name = record["name"].ToString();
        
this._birthday = record["birthday"].ToString();
    }

}

Service.cs

using  System;
using  System.Web;
using  System.Web.Services;
using  System.Web.Services.Protocols;
using  System.Data;
using  System.Data.SqlClient;
using  System.Data.Sql;
using  System.Data.SqlTypes;
using  System.Collections;

[WebService(Namespace 
=   " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
public   class  Service : System.Web.Services.WebService
{
    
public const string ConnString = @"Data Source=.SQLEXPRESS;AttachDbFilename=C:Documents and SettingszhouluMy DocumentsVisual Studio 2005WebSiteswsApp_DataFamily.mdf;Integrated Security=True;User Instance=True";
    
public Service () {

        
//如果使用设计的组件,请取消注释以下行 
        
//InitializeComponent(); 
    }


    [WebMethod]
    
public string HelloWorld() {
        
return "Hello World";
    }


    [WebMethod]
    
public Person[] FindPerson(string Name)
    
{
        ArrayList result 
= new ArrayList();
        
using(SqlConnection conn = new SqlConnection(ConnString))
        
using (SqlCommand dataCommand = new SqlCommand("dbo.findbyname", conn))
        
{
            
//dataCommand.Parameters.Add("@name", name);
            dataCommand.CommandType = CommandType.StoredProcedure;
            dataCommand.Parameters.AddWithValue(
"@name", Name);
            SqlDataAdapter dataAdapter 
= new SqlDataAdapter(dataCommand);
            DataTable data 
= new DataTable();
            dataAdapter.Fill(data);

            
for (int i = 0; i < data.Rows.Count; i++)
            
{
                result.Add(
new Person(data.Rows[i]));
            }

        }

        
return (Person[])result.ToArray(typeof(Person));///问题所在
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
回答: 有两个数组方法可以返回一个新数组。第一个是数组的slice()方法,它可以将一个数组分割成另一个新数组返回一个新的数组对象,不会改变原数组。slice()方法接受两个参数,分别是开始索引和结束索引,包含开始索引,不包含结束索引。如果不传入参数,默认从索引0开始,到数组末尾结束,包含末尾。另一个方法是数组的concat()方法,它可以将多个数组合并成一个新数组,并返回这个新数组,不会改变原数组。concat()方法可以接受多个参数,每个参数都是一个数组。这两个方法都可以返回一个新数组,满足题目要求。\[2\] #### 引用[.reference_title] - *1* *2* [删除数组arr最后一个元素。不要接修改数组arr,结果返回新的数组](https://blog.csdn.net/qq_41131745/article/details/114659931)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [对数组操作的几种常见方法及其返回值](https://blog.csdn.net/qq_41996062/article/details/110368398)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值