【问题标题】:Reading data from MySQL to VB从 MySQL 读取数据到 VB
【发布时间】:2016-06-08 23:04:53
【问题描述】:

我只是在使用 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

我只是简单地用.. 选择商店(shopID)

“返回”语句只允许我传回一个字段。请问如何将所有字段传回以在代码中使用?

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

【问题讨论】:

    标签: mysql asp.net vb.net


    【解决方案1】:

    创建一个类“商店”

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

    将函数返回类型更改为 Shop

    Public Function SelectShop(ByVal orderString As Guid) As Shop
    

    更新函数代码sn-p如下:

     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
    

    【讨论】:

    • 很高兴,如果有帮助,如果是这种情况,请接受作为答案。
    • 非常感谢,这有很多帮助.. 差不多了.. 现在只需要知道如何在代码中声明它。我还需要使用 RETURN 吗?作为测试,我尝试做 RESPONSE.WRITE(OBJSHOP.POSTCODE) 并且它说没有声明 objshop。
    • 一种选择是为 Shop 类创建一个新的类文件 (Shop.vb)。然后在现有函数 SelectShop 中调用该类。是的,仍然需要返回 Shop 对象。
    • 再次感谢萨米。我会把什么作为 Return ?我试过退货店,但它不喜欢它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多