需要将字符串列表添加到Web服务中

时间:2013-04-16 22:35:16

标签: asp.net sql-server vb.net visual-studio-2010 web-services

我正在通过asp.net创建一个Web服务。我想把列表中的燃料停止的地址作为一个列表,如下所示,不幸的是我得到的是地址的字符串名称,如下图所示...

不幸的是,当我运行这个时,我得到一个填充

的xml文件
-<FuelStop> 
    <Physical_Address_Street>Physical_Address_Street</Physical_Address_Street>                   
    <Physical_Address_Local>Physical_Address_Local</Physical_Address_Local> 
    <Physical_Address_State>Physical_Address_State</Physical_Address_State>           
    <Physical_Address_Zip>Physical_Address_Zip</Physical_Address_Zip> 
    <Phone_Number>Phone_Number</Phone_Number> 
</FuelStop>

获取填充在数据库中的地址信息。

以下是我的代码。

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient

<System.Web.Services.WebService(Namespace:="http://watersports.com 8010/",     Description:="Holds Fuel Stop and Shelter information", Name:="ShelterandFuelService")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function GetAddresses(ByVal skip As Integer, ByVal take As Integer) As FuelStop()

    Dim sqlCon As New SqlConnection
    Dim resultList = New List(Of FuelStop)()


    Try

        sqlCon.ConnectionString = "Data Source=google.watersports.com;Initial Catalog=myDb;Persist Security Info=True;Connect Timeout=30;User ID=****;Password=******"
                   Dim command As New SqlCommand("SELECT @Physical_Address_Street, @Physical_Address_Local, @Physical_Address_State, @Physical_Address_Zip, @Phone_Number FROM Gas_Stations WHERE Location_Type = 1")
        command.Parameters.Add("@Physical_Address_Street", SqlDbType.VarChar, 50).Value = "Physical_Address_Street"
        command.Parameters.Add("@Physical_Address_Local", SqlDbType.VarChar, 50).Value = "Physical_Address_Local"
        command.Parameters.Add("@Physical_Address_State", SqlDbType.VarChar, 50).Value = "Physical_Address_State"
        command.Parameters.Add("@Physical_Address_Zip", SqlDbType.VarChar, 50).Value = "Physical_Address_Zip"
        command.Parameters.Add("@Phone_Number", SqlDbType.VarChar, 50).Value = "Phone_Number"

        command.Connection = sqlCon
        sqlCon.Open()
        'command.ExecuteNonQuery()
        Using reader = command.ExecuteReader()
            While reader.Read()
                Dim fuelStop = New FuelStop()
                fuelStop.Physical_Address_Street = reader.GetString(0)
                fuelStop.Physical_Address_Local = reader.GetString(1)
                fuelStop.Physical_Address_State = reader.GetString(2)
                fuelStop.Physical_Address_Zip = reader.GetString(3)
                fuelStop.Phone_Number = reader.GetString(4)

                resultList.Add(fuelStop)


            End While
        End Using

    Catch ex As Exception

        sqlCon.Close()
    Finally
        sqlCon.Close()

    End Try
    Return resultList.Skip(skip).Take(take).ToArray()

根据下面的Sql命令,我可能会返回几百个地址,如何将其合并到我的vb.net逻辑中以返回地址列表?

Dim command As New SqlCommand("SELECT Physical_Address_Street, Physical_Address_Local, Physical_Address_State, Physical_Address_Zip, Phone_Number FROM Gas_Stations WHERE Location_Type = 1")

这是我的燃料停止等级

Public Class FuelStop

    Property Physical_Address_Street As String

    Property Physical_Address_Local As String

    Property Physical_Address_State As String

    Property Physical_Address_Zip As String

    Property Phone_Number As String

End Class

1 个答案:

答案 0 :(得分:1)

以下代码应该可以满足您的需求。我不确定WebMethods是否可以返回自定义对象,如下面的Address对象。如果它不能这样做,那么你可以将返回类型转换为String()并将每个Address对象转换为json对象。

如果我从头开始这样做,我只会返回一个包含Address对象列表的JSON数组。

另外,我在此方法签名中包含了skip和take参数,正如您在评论中提到的,Web服务sql查询是无限制的,因此它可以返回数百个地址。通过使用skip和take,您可以限制一次返回的数量,并可以从客户端进行翻页。要减小sql查询的大小,可以传递skip并将参数带入sql查询以减少返回的行数,这样可以在地址数量增加时提高性能。

您可以在下面的代码中包含其他安全检查,以防止出现错误,但这可能会让您感动。

<WebMethod> _
Public Function GetAddresses(skip As Integer, take As Integer) As Address()
    Dim resultList = New List(Of Address)()
    Using sqlCon As New SqlConnection()
            sqlCon.ConnectionString = "Data Source=google.watersports.com;Initial Catalog=myDb;Persist Security Info=True;Connect Timeout=30;User ID=****;Password=******"
            Dim command As New SqlCommand("SELECT Physical_Address_Street, Physical_Address_Local, Physical_Address_State, Physical_Address_Zip, Phone_Number FROM Gas_Stations WHERE Location_Type = 1")

            sqlCon.Open()
            Using reader = command.ExecuteReader()
                While reader.Read()
                    Dim addr = New Address()
                    addr.Physical_Address_Street = reader.GetString(0)
                    addr.Physical_Address_Local = reader.GetString(1)
                    addr.Physical_Address_State = reader.GetString(2)
                    addr.Physical_Address_Zip = reader.GetString(3)
                    addr.Phone_Number = reader.GetString(4)

                    resultList.Add(addr)
                End While
            End Using
    End Using
    Return resultList.Skip(skip).Take(take).ToArray()
End Function

下面是上面使用的临时类对象。

public class Address
{
    public string Physical_Address_Street { get; set; }
    public string Physical_Address_Local { get; set; }
    public string Physical_Address_State { get; set; }
    public string Physical_Address_Zip { get; set; }
    public string Phone_Number { get; set; }
}