2

我想从 Web 服务内的数据集中检索数据到我的 Windows 窗体,然后将数据从我的 Windows 窗体发送到 Web 服务。

我定义了一个函数,它在我的 asmx 文件中的一个类中返回一个数据集:

Public Class DataSetWebService

    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function GetUsers() As DataSet
        Dim cn As SqlConnection = New SqlConnection("Data Source=.;Initial Catalog=WebServiceTest;Integrated Security=SSPI")
        Dim cm As SqlCommand : Dim ds As New DataSet : Dim da As SqlDataAdapter : Dim str As String  
        Try
            str = "Select * from User_Table"
            If cn.State = ConnectionState.Closed Then cn.Open()
            cm = New SqlCommand(str, cn)
            da = New SqlDataAdapter(cm)
            da.Fill(ds)
            return ds
        Catch ex As SqlException
        Catch ex As Exception
        Finally
            If cn.State = ConnectionState.Open Then cn.Close()
        End Try
    End Function

End Class

在我的 Windows 窗体中,我编写了以下代码:

  Dim ws As New UserService.DataSetWebService()    

但它说“DataSetWebService() 未定义”

4

1 回答 1

3

您需要将服务方法定义为Shared。像这样

Public Shared Function GetUsers() As DataSet

然后将“添加 Web 引用”添加到您的 Windows 应用程序。

希望它能解决你的问题。

于 2013-09-13T11:41:40.397 回答