Sql 将web服务连接到数据库并显示信息

Sql 将web服务连接到数据库并显示信息,sql,sql-server,vb.net,web-services,Sql,Sql Server,Vb.net,Web Services,我是一名VB.Net初学者,想知道如何创建web服务,并将其连接到SQL 2012数据库,以及如何在表中显示项目。数据库表有多个字段,包括库存项目、制造商、自由库存数量、价格等 如果我添加一个断点并逐步遍历代码,我可以看到一个值被传递到记录集中的一个字符串中并返回,但是,这不会正确显示。显然,我已经更改了服务器、用户名和密码,但这是下面显示的代码 Public Class Service1 Inherits System.Web.Services.WebService Private sqlc

我是一名VB.Net初学者,想知道如何创建web服务,并将其连接到SQL 2012数据库,以及如何在表中显示项目。数据库表有多个字段,包括库存项目、制造商、自由库存数量、价格等

如果我添加一个断点并逐步遍历代码,我可以看到一个值被传递到记录集中的一个字符串中并返回,但是,这不会正确显示。显然,我已经更改了服务器、用户名和密码,但这是下面显示的代码

Public Class Service1
Inherits System.Web.Services.WebService

Private sqlconnection As ADODB.Connection

<WebMethod()> _
Public Function ShowStockItems() As String

    Dim Command As New SqlCommand

    sqlconnection = New ADODB.Connection
    sqlconnection.ConnectionString = "driver={SQL Server};server=server;uid=id;pwd=password;database=Able_Instruments"
    Try
        sqlconnection.Open()
        Dim strReturn As String = ""
        Dim rstStockItem As New ADODB.Recordset
        Dim strStockItem As String = "Select * from StockItem"
        rstStockItem.Open(strStockItem, sqlconnection)
        With rstStockItem
            If Not .BOF Then
                .MoveFirst()
                strReturn = .Fields.Item("TaxCodeID").Value
            End If![enter image description here][1]
        End With
        rstStockItem.Close()
        sqlconnection.Close()
        'Return strReturn
    Catch ex As Exception
    Finally
    End Try
End Function
End Class
我不确定问题是否明显,但我希望能够显示库存项目的ID和库存项目的名称。对于这个例子,我使用TaxCodeID来确保代码正常工作。使用Visual Studio 2010,如果需要,还可以访问2013


谢谢你的帮助

当我不使用实体框架时,我喜欢为复杂的数据检索构建类

Public Class StockItem
  Public Property Name As String
  Public Property ID As Integer
  'others as needed
End Class 
用法:

<WebMethod()> _
Public Function ShowStockItems() As StockItem
 Dim _stockItem As New StockItem
 Dim Command As New SqlCommand
 sqlconnection = New ADODB.Connection
 sqlconnection.ConnectionString = "driver={SQL Server};server=server;uid=id;pwd=password;database=Able_Instruments"
 Try
    sqlconnection.Open()
    Dim strReturn As String = ""
    Dim rstStockItem As New ADODB.Recordset
    Dim strStockItem As String = "Select * from StockItem"
    rstStockItem.Open(strStockItem, sqlconnection)
    With rstStockItem
        If Not .BOF Then
            .MoveFirst()
            _stockItem.ID = Convert.ToInt32(.Fields.Item("TaxCodeID").Value)
            _stockItem.Name = .Fields.Item("what field you need).Value
        End If
    End With
 Catch
 Finally
   rstStockItem.Close()
   sqlconnection.Close()
 End Try
 Return _stockItem
End Function

感谢您的回复,非常有帮助!如果这与答案相符,则单击复选标记,并在适当的情况下向上投票。