sql/vb.net

siv3szwd  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(387)

我有一个表,它总是只有3行,每周更新。我需要阅读他们和填充3个imageurl的和3个相关的超链接。但是,我得到了一个错误:

System.IndexOutOfRangeException: hyperlink1

我可能离这里太远了,因为我还在学习,但我们走了。。我只是用。。

SELECT 
    (SELECT hyperlink As hyperlink1 WHERE productNo=1),
    (SELECT imageUrl As imageUrl1 WHERE productNo=1),
    (SELECT hyperlink As hyperlink2 WHERE productNo=2),
    (SELECT imageUrl As imageUrl2 WHERE productNo=2),
    (SELECT hyperlink As hyperlink3  WHERE productNo=3),
    (SELECT imageUrl As imageUrl3 WHERE productNo=3)

从TBL产品端
然后vb代码。。

Public Function GetAmazonProducts()
        Dim DBConnectAm As New DBConn
        Using db As DbConnection = DBConnectAm.Conn("DBConnectionString")
            Dim cmd As SqlCommand = DBConnectAm.Command(db, "SelectAmazonProducts")
            'cmd.Parameters.Add(New SqlParameter("productNo", SqlDbType.Int, ParameterDirection.Input)).Value = productNo
            db.Open()
            Dim DR As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                hypAmazonProduct1.navigateurl=DR("hyperlink1")
                imghypAmazonProduct1.ImageUrl=DR("imageUrl1")
                hypAmazonProduct2.navigateurl=DR("hyperlink2")
                imghypAmazonProduct2.ImageUrl=DR("imageUrl2")
                hypAmazonProduct3.navigateurl=DR("hyperlink3")
                imghypAmazonProduct3.ImageUrl=DR("imageUrl3")
            DR.Close()
            DR = Nothing
            cmd.Dispose()
            cmd = Nothing
            db.Dispose()
            db.Close()
        End Using
   End Function
8xiog9wr

8xiog9wr1#

我将do while放回原处,然后在每行中抖动select语句,使其看起来像这样:

SELECT 
    (SELECT hyperlink FROM tblAmazonProducts WHERE productNo=1)As hyperlink1,
    (SELECT imageUrl FROM tblAmazonProducts WHERE productNo=1) As imageUrl1,
    (SELECT hyperlink FROM tblAmazonProducts WHERE productNo=2) As hyperlink2,
    (SELECT imageUrl FROM tblAmazonProducts WHERE productNo=2) As imageUrl2,
    (SELECT hyperlink FROM tblAmazonProducts  WHERE productNo=3) As hyperlink3,
    (SELECT imageUrl FROM tblAmazonProducts WHERE productNo=3) As imageUrl3

vb。。

Dim DBConnectAm As New DBConn
    Using db As DbConnection = DBConnectAm.Conn("DBConnectionString")
        Dim cmd As SqlCommand = DBConnectAm.Command(db, "SelectAmazonProducts")
        'cmd.Parameters.Add(New SqlParameter("productNo", SqlDbType.Int, ParameterDirection.Input)).Value = productNo
        db.Open()
        Dim DR As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        While DR.Read
            hypAmazonProduct1.navigateurl=DR("hyperlink1")
            imghypAmazonProduct1.ImageUrl=DR("imageUrl1")
            hypAmazonProduct2.navigateurl=DR("hyperlink2")
            imghypAmazonProduct2.ImageUrl=DR("imageUrl2")
            hypAmazonProduct3.navigateurl=DR("hyperlink3")
            imghypAmazonProduct3.ImageUrl=DR("imageUrl3")
        End While
        DR.Close()
        DR = Nothing
        cmd.Dispose()
        cmd = Nothing
        db.Dispose()
        db.Close()
    End Using

相关问题