Web服务在一行中全部返回数据

本文关键字:一行 全部 返回 数据 服务 Web | 更新日期: 2023-09-27 18:00:00

所以我有一个从MSSQL数据库获取数据的基本web服务。在SQL连接和查询方面,它可以正常工作。但是,我想知道是否可以更优雅地显示数据。我的目标是在每行上返回一个名称,但web服务只是在字符串标记和一行上的所有名称之间吐出所有名称。我对C#和ASP.NET非常陌生,能走到这一步我感到非常惊讶。如果可能的话,我只需要以一种更愉快的方式格式化数据。

这是我的代码

namespace CustomerService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX,  uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

    [WebMethod]
    //create new webMethod to get names
    public string getNames()
    {
        int size = DataHelper.name().Count();
        string[] names = new string[size];
        names = DataHelper.name();
        return toString(names);
    }
    //take array passed in and convert to one single string
    public string toString(string[] names)
    {
        int size = names.Count();
        string[] nameArray = new string[size];
        nameArray = names;
        string output = "";
        for (int x = 0; x < size; x++)
        {
            output = output + "'n" + nameArray[x];
        }
        return output;
    }
}
}

然后是DataHelper服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace CustomerService
{
    public class DataHelper
    {
    //create method to get names from customer DB
    public static string[] name()
    {
        string currentName ="";
        string[] names = new string[100];
        double checkingBal;
        double savingsBal;
        double cdBal;
        double mmBal;
        //create connection
        SqlConnection conn = new SqlConnection(@"Data Source=STE2074;Initial Catalog=ATMWeb;Persist Security Info=True;User ID=stp;Password=stp48329");
        //create command to get names
        string sqlString = "SELECT * FROM tblUsers";
        SqlCommand cmd = new SqlCommand(sqlString, conn);
        conn.Open();
        int x = 0;
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                currentName = reader["firstName"].ToString();
                names[x] = currentName;
                x++;
            }
            //close connections
            conn.Close();
            reader.Close();
            return names;
        }  
    }
}

这就是最终结果:

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/"> Stephen lisa steve kyle s Lisa steven Customer   chelsea jon karen jessica meagan </string>

我想在每个名字后面加一行。。。这可能吗?

Web服务在一行中全部返回数据

创建一个类并返回。

PUBLIC Class MyOldStringNames
{
   property string NameHolder { get; set; }
}
End Class
List<MyOldStringNames> myListOfOldStringNames = new List<MyOldStringNames>();
For Each item in names 
   MyOldStringNames myItemToAdd = new MyOldStringNames() { NameHolder = item.toString() };
   myListOfOldStringNames.add(myItemToAdd);
End
return myListOfOldStringNames;

当然,你的方法是这样的

public List<MyOldStringNames> getNames()
    {
        int size = DataHelper.name().Count();
        string[] names = new string[size];
        names = DataHelper.name();
        //Do stuff above
        //return data as shown above;
    }