2016-06-08 72 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