需要得到一个字符串列表到一个web服务 [英] Needing to get a list of strings into a webservice

查看:94
本文介绍了需要得到一个字符串列表到一个web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建通过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>

对战获得了在数据库中填充的地址信息。

下面是我的code。

 导入System.Web.Services
进口System.Web.Services.Protocols
进口System.ComponentModel
进口System.Data这
进口System.Data.SqlClient的< System.Web.Services.WebService(命名空间:=htt​​p://watersports.com 8010 /,说明:=举行燃油停止和住房的信息,名称:=ShelterandFuelService)> _
< System.Web.Services.WebServiceBinding(ConformsTo:= WsiProfiles.BasicProfile1_1)GT; _
<的ToolboxItem(假)> _
公共类服务
继承System.Web.Services.WebService&所述;的WebMethod()> _
公共职能GetAddresses(BYVAL跳过整数,BYVAL采取作为整数)作为FuelStop()    昏暗sqlCon作为新的SqlConnection
    昏暗的resultList =新名单(中FuelStop)()
    尝试        sqlCon.ConnectionString =数据源= google.watersports.com;初始目录= MYDB;坚持安全信息= TRUE;连接超时= 30;用户ID = ****;密码= ******
                   昏暗的命令作为新的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)。价值=Physical_Address_Street
        command.Parameters.Add(@ Physical_Address_Local,SqlDbType.VarChar,50)。价值=Physical_Address_Local
        command.Parameters.Add(@ Physical_Address_State,SqlDbType.VarChar,50)。价值=Physical_Address_State
        command.Parameters.Add(@ Physical_Address_Zip,SqlDbType.VarChar,50)。价值=Physical_Address_Zip
        command.Parameters.Add(@ PHONE_NUMBER,SqlDbType.VarChar,50).value的=PHONE_NUMBER        command.Connection = sqlCon
        sqlCon.Open()
        command.ExecuteNonQuery()
        使用读卡器= Command.ExecuteReader却()
            虽然reader.Read()
                昏暗fuelStop =新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)
            虽然结束
        使用完    抓住EX为例外        sqlCon.Close()
    最后
        sqlCon.Close()    结束Try
    返回resultList.Skip(跳过)。取(取).ToArray()

根据SQL命令下面我将有可能回到几百地址的我怎么incorporparate到我的vb.net逻辑此返回地址的列表?

 昏暗的命令作为新的SqlCommand(SELECT Physical_Address_Street,Physical_Address_Local,Physical_Address_State,Physical_Address_Zip,从PHONE_NUMBER WHERE Gas_Stations LOCATION_TYPE = 1)

这是我的燃料停止类

 公共类FuelStop    物业Physical_Address_Street作为字符串    物业Physical_Address_Local作为字符串    物业Physical_Address_State作为字符串    物业Physical_Address_Zip作为字符串    物业PHONE_NUMBER作为字符串末级


解决方案

下面code应该给你你在找什么。我不知道,如果能的WebMethods返回类似下面的地址对象自定义对象。如果不能做到这一点,那么你可以转换成返回类型为String()和将每个地址对象转换为JSON对象。

如果我是从头开始做这个,我只想返回包含地址对象列表中的单个JSON数组

另外,我在这个方法的签名包括跳跃,并采取参数,如您在您的评论中提到,Web服务的SQL查询是无界的,因此它可能返回数百个地址。通过使用跳跃,走你可以限制多少是在一次从客户端返回,并且可以通过他们的页面。为了减少SQL查询的大小,你可以通过跳跃,并采取参数到SQL查询,以减少行数返回,这将提高性能的地址数量的增长。

有额外的安全检查,您可以在以下code至prevent错误,但这样可以让你感动。

 <的WebMethod> _
公共职能GetAddresses(跳过整数,取为整数)由于地址()
    昏暗resultList =新名单(地址)()
    使用sqlCon作为新的SqlConnection()
            sqlCon.ConnectionString =数据源= google.watersports.com;初始目录= MYDB;坚持安全信息= TRUE;连接超时= 30;用户ID = ****;密码= ******
            昏暗的命令作为新的SqlCommand(SELECT Physical_Address_Street,Physical_Address_Local,Physical_Address_State,Physical_Address_Zip,从PHONE_NUMBER WHERE Gas_Stations LOCATION_TYPE = 1)            sqlCon.Open()
            使用读卡器= Command.ExecuteReader却()
                虽然reader.Read()
                    昏暗的地址=新地址()
                    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(地址)
                虽然结束
            使用完
    使用完
    返回resultList.Skip(跳过)。取(取).ToArray()
结束功能

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

 公共类地址
{
    公共字符串Physical_Address_Street {搞定;组; }
    公共字符串Physical_Address_Local {搞定;组; }
    公共字符串Physical_Address_State {搞定;组; }
    公共字符串Physical_Address_Zip {搞定;组; }
    公共字符串PHONE_NUMBER {搞定;组; }
}

I am creating a web service via asp.net. I want to pull as a list the Address's of the Fuel Stops as shown below unfortunately what I am getting is the string name of address's as shown below from the xml...

Unfortunately when I do run this I get a xml file filled with

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

Versus getting the address info that is populated in the database.

Below is my code.

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()

Based on the Sql Command below I will return possibly a few hundred address's how do I incorporparate this into my vb.net logic to return the list of address's?

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")

And here is my Fuel stop class

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

解决方案

The below code should give you what you are looking for. I am not sure if WebMethods can return custom objects like the Address object below. If it can not do that then you could convert the return type to String() and turn each Address object into a json object.

If I was doing this from scratch, I would just return a single JSON array that contained a list of Address objects.

Also, I have included in this method signature a skip and take parameter, as you mentioned in your comment, the web service sql query is unbounded so it could return hundreds of addresses. By using the skip and take you can limit how many are returned at once and can page through them from the client. To reduce the size of the sql query you could pass the skip and take parameters into the sql query to reduce the number of rows returned, which would improve performance as the number of addresses grows.

There are additional safety checks you can include in the below code to prevent errors but this can get you moving.

<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

Below is a temp class object that is used above.

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

这篇关于需要得到一个字符串列表到一个web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆