Web服务在一条线上返回所有数据

时间:2014-04-24 00:01:35

标签: c# asmx

所以我有一个从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>

我希望每个名字后面都有一个新行......这可能吗?

1 个答案:

答案 0 :(得分:0)

创建一个类并返回该类。

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;
    }