0

我正在通过 asp.net 创建一个 Web 服务。我想将加油站的地址作为一个列表拉出来,如下所示不幸的是,我得到的是地址的字符串名称,如下所示来自 xml...

不幸的是,当我运行它时,我得到一个 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
4

1 回答 1

1

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

如果我从头开始执行此操作,我将只返回一个包含 Address 对象列表的 JSON 数组。

此外,我在此方法签名中包含了一个跳过和获取参数,正如您在评论中提到的,Web 服务 sql 查询是无界的,因此它可以返回数百个地址。通过使用skip and take,您可以限制一次返回的数量,并可以从客户端对它们进行分页。要减小 sql 查询的大小,您可以将 skip 和 take 参数传递到 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; }
}
于 2013-04-16T23:25:35.647 回答