2014-04-24 1 views
1

그래서 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> 

내가 각 후 새로운 라인을 싶습니다 이름이 ... 가능하니?

+2

XML은 공백을 지원하지 않습니다. ('xml : space' 제외) – SLaks

+0

컬렉션을 돌려 보내지 않는 이유는 무엇입니까? 여기서 아주 분명합니다. –

+0

여기에 컬렉션을 반환하는 방법은 무엇입니까? 죄송합니다 ... 다시 한 번, 나는 아주 새로운 lol입니다 –

답변

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

VB 용 클래스 선언입니까? 나는 C#에서 동등한 것을 찾고있다. C#에서 해당 클래스를 만들려고하면 컴파일되지 않습니다. 다음은 C#에서했던 것입니다. public class MyOldStringNames { string NameHolder {get; 세트; } } –

+0

방금 ​​비행기에서 썼다면, 괄호가 누락되었습니다. –

+0

감사합니다. 나는 거의 거기에 있지만 오류가 발생한다. "일관성없는 접근성 : 반환 형식 'System.Collections.Generic.List '는 'CustomerService.Service1.getNames()'메서드보다 액세스하기가 쉽지 않다. –

관련 문제