그래서 MSSQL 데이터베이스에서 데이터를 가져 오는 기본 웹 서비스가 있습니다. 그것은 SQL 연결 및 쿼리의 관점에서 가정 된대로 작동하지만, 데이터를 좀 더 우아하게 표시 할 수 있는지 알고 싶습니다. 내 목표는 각 줄에 하나의 이름을 반환하는 것입니다. 그러나 웹 서비스는 문자열 태그와 모든 줄 사이에 모든 것을 뱉어냅니다. 나는 C#과 ASP.NET에 익숙하지 않은데, 지금까지 만들었습니다. 가능한 경우 데이터를보다 즐겁게 형식화해야합니다. 여기 Webservice가 모두 한 줄에 데이터를 반환합니다.
는 DataHelper 서비스 다음 내 코드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;
}
}
}
그리고있다 :
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>
내가 각 후 새로운 라인을 싶습니다 이름이 ... 가능하니?
XML은 공백을 지원하지 않습니다. ('xml : space' 제외) – SLaks
컬렉션을 돌려 보내지 않는 이유는 무엇입니까? 여기서 아주 분명합니다. –
여기에 컬렉션을 반환하는 방법은 무엇입니까? 죄송합니다 ... 다시 한 번, 나는 아주 새로운 lol입니다 –