2016-06-08 70 views
2

我只是學習和使用Asp.Net/VB/SQL網站上工作..我試圖從SQL中讀取一整行數據來操縱我的VB代碼。我到目前爲止有這..從MySQL讀取數據到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

創建一個類 「商店」

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 
+0

快樂是否有幫助,請接受作爲答案,如果是這樣的話。 – Sami

+0

非常感謝,這有助於很多..幾乎在那裏..只需要知道如何在代碼中聲明它。我是否還需要使用RETURN?作爲一個測試,我試着做RESPONSE.WRITE(OBJSHOP.POSTCODE),它說objshop沒有聲明。 – Eggybread

+0

一個選項可能是爲Shop類創建一個新的類文件(Shop.vb)。然後在你現有的SelectShop函數中調用這個類。是的,仍然需要返回Shop對象。 – Sami