繁体   English   中英

如何将存储过程的结果作为XML返回?

[英]How can I return a stored procedure's result as XML?

我对连接到MS-Sql服务器并从存储过程获取结果的Web服务进行了编码。 我下面的代码仅获得过程的第一个结果,但是我需要响应xml文件中的所有结果。 我认为我需要使用数据适配器,数据读取器,数据填充等。 但是我不能成功。 我将不胜感激任何帮助:

PS:Info.cs类已创建。

[WebMethod]
public Info NumberSearch2(string no)
{
    Info ourInfo = new Info();
    string cs = ConfigurationManager.ConnectionStrings["DBBaglan"].ConnectionString;

    using (SqlConnection Con = new SqlConnection(cs))
    {
        SqlCommand cmd = new SqlCommand(cmdText: "NumberSearch", connection: Con)
        {
            CommandType = CommandType.StoredProcedure
        };

        SqlParameter parameter = new SqlParameter
        {
            ParameterName = "@no",
            Value = no
        };

        cmd.Parameters.Add(parameter);
        Con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        DataSet ds = new DataSet();

        while (dr.Read())
        {
            ourInfo.PartNo = dr["PartNo"].ToString();
            ourInfo.BrandNo = dr["BrandNo"].ToString();
            ourInfo.Manufacturer = dr["Manufacturer"].ToString();
            ourInfo.Country = dr["Country"].ToString();
            ourInfo.ReferenceNo = dr["ReferenceNo"].ToString();
        }
    }

    return ourInfo;
}

经过@David的推荐:

    [WebMethod]
    //public Info NumberSearch2(string no)
    public List<Info> NumberSearch2(string no)
    {
        Info ourInfo = new Info();
        var ourInfos = new List<Info>();

        string cs = System.Configuration.ConfigurationManager.ConnectionStrings["DBBaglan"].ConnectionString;
        using (System.Data.SqlClient.SqlConnection Con = new System.Data.SqlClient.SqlConnection(cs))
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(cmdText: "NumberSearch", connection: Con)
            {
                CommandType = System.Data.CommandType.StoredProcedure
            };

            System.Data.SqlClient.SqlParameter parameter = new System.Data.SqlClient.SqlParameter
            {
                ParameterName = "@no",
                Value = no
            };

            cmd.Parameters.Add(parameter);
            Con.Open();
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
            DataSet ds = new DataSet();
            while (dr.Read())

            {
                var ourInfos = new Info();
                ourInfo.PartNo = dr["PartNo"].ToString();
                ourInfo.BrandNo = dr["BrandNo"].ToString();
                ourInfo.Manufacturer = dr["Manufacturer"].ToString();
                ourInfo.Country = dr["Country"].ToString();
                ourInfo.ReferenceNo = dr["ReferenceNo"].ToString();
                ourInfos.Add(ourInfo);
            }
        }

        return ourInfos;

    }

您将返回一个Info对象。 如果要收集它们,则返回List<Info> 更改方法签名:

public List<Info> NumberSearch2(string no)

声明要返回的对象:

var ourInfos = new List<Info>();

在循环中,将每个记录添加到列表中:

while (dr.Read())
{
    var ourInfo = new Info();
    ourInfo.PartNo = dr["PartNo"].ToString();
    ourInfo.BrandNo = dr["BrandNo"].ToString();
    ourInfo.Manufacturer = dr["Manufacturer"].ToString();
    ourInfo.Country = dr["Country"].ToString();
    ourInfo.ReferenceNo = dr["ReferenceNo"].ToString();
    ourInfos.Add(ourInfo);
}

并返回列表:

return ourInfos;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM