从MySQL读取数据到VB

时间:2016-06-08 23:04:54

标签: mysql asp.net vb.net

我正在使用Asp.Net/VB/SQL学习和使用网站。我试图从SQL中读取一整行数据来操作我的VB代码。到目前为止,我有这个......

BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

SELECT  shopName
       ,shopLogo
       ,addressLine1
       ,city
       ,postcode
       ,phoneNumber
       ,pointsPerPound
       ,maxPoints
       ,info

FROM tblShopKeeper
WHERE orderID = @shopID
END

选择数据但是如何将其传回VB? 我有这个代码..

Public Function SelectShop(ByVal orderString As Guid) As String
    Dim DBConnect As New DBConn
    Using db As DbConnection = DBConnect.Conn("DBConnectionString")
        Dim cmd As SqlCommand = DBConnect.Command(db, "SelectShop")
        cmd.Parameters.Add(New SqlParameter("shopID",         SqlDbType.uniqueIdentifier, ParameterDirection.Input)).Value = orderString

        db.Open()
        Dim shopName As String
        Dim shopLogo As String
        Dim addressLine1 As String
        Dim city As String
        Dim postcode As String
        Dim phoneNumber As String
        Dim pointsPerPound As Integer
        Dim maxPoints As Integer
        Dim info As String

        Dim DR As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

        While DR.Read
            shopName = DR("shopName")
            shopLogo = DR("shopLogo")
            addressLine1 = DR("addressLine1")
            city = DR("city")
            postcode = DR("postcode")
            phoneNumber = DR("phoneNumber")
            pointsPerPound = DR("pointsPerPound")
            maxPoints = DR("maxPoints")
            info = DR("info")
        End While

        DR.Close()
        DR = Nothing
        cmd.Dispose()
        cmd = Nothing
        db.Dispose()
        db.Close()

        Return shopName
    End Using
End Function

我只是简单地称之为.. SelectShop(shopID)

"返回"语句只允许我传回一个字段..如何将所有字段传回代码中使用?

要温柔..我只是一个新手:-) 非常感谢。

1 个答案:

答案 0 :(得分:1)

创建课程" Shop"

Public Class Shop

   public property ShopName as String
   public property ShopLogo as String
   public property addressLine1 as String
   ...
End Class

将功能返回类型更改为商店

Public Function SelectShop(ByVal orderString As Guid) As Shop

更新功能代码段如下:

 Dim DR As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

 Dim objShop as New Shop()

    While DR.Read
        objShop.ShopName = DR("shopName")
        objShop.ShopLogo = DR("shopLogo")
        ...    

    End While

... 'Your code

    Return shopName