2014-01-16 38 views
1

我想要一個循環查詢結果並將它們顯示在Visual Basic的列表框中的代碼。到目前爲止我所擁有的是一個存儲過程,一個列表框和一個按鈕。Visual Basic循環查詢結果

存儲過程IM調用被稱爲「CycleCustomers」和在SQL如下:

ALTER PROCEDURE CycleCustomers 
-- Add the parameters for the stored procedure here 
@p1 int 

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

-- Insert statements for procedure here 
SELECT FirstName from tblCustomer where CustomerId = @p1 
END 

上按一下按鈕的代碼,我有如下

Dim dc = New DataTestDataContext 
    Dim DealerCount As Int32 
    DealerCount = 0 
    ListBox2.Items.Clear() 

    Do Until DealerCount = 10 
     dc.CycleCustomers(DealerCount) 
     ListBox2.Items.Add(dc.CycleCustomers(DealerCount).ReturnValue) 
     DealerCount = DealerCount + 1 
    Loop 

當按鈕被點擊,我的列表框返回10個零。我究竟做錯了什麼?提前致謝。

編輯:這裏是CYCLECUSTOMERS功能

<FunctionAttribute(Name:="dbo.CycleCustomers")> _ 
Public Function CycleCustomers(<Parameter(DbType:="Int")> ByVal p1 As System.Nullable(Of Integer)) As ISingleResult(Of CycleCustomersResult1) 
    Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod,MethodInfo), p1) 
    Return CType(result.ReturnValue,ISingleResult(Of CycleCustomersResult1)) 
End Function 
+0

向我們展示「CycleCustomers」方法以瞭解其工作原理。 – user2989408

+0

你通常有一個'SELECT'查詢並通過使用'DataReader'循環。 – Neolisk

+0

CycleCustomers是一個存儲過程,我把它的代碼第一個 – Zingo

回答

2

看一看這裏:http://msdn.microsoft.com/en-us/library/bb534556(v=vs.110).aspx

你所取回的類型是CycleCustomerResult1,這是一個IEnumerable的ISingleResult。你應該能夠使用LINQ做的是一樣的東西:

Dim dc = New DataTestDataContext 
Dim DealerCount As Int32 
DealerCount = 0 
ListBox2.Items.Clear() 

Do Until DealerCount = 10 
    Dim Customer = dc.CycleCustomers(DealerCount).SingleOrDefault() 

    If (Not(Customer Is Nothing)) 
    ListBox2.Items.Add(Customer.FirstName) 
    End If 

    DealerCount = DealerCount + 1 

Loop 

我沒有Visual Studio中的時刻進行檢查:還我一般使用C#,所以我的VB的精度可能是窮人。

但是,無論如何,你採取的方法似乎非常非正統。理想情況下,您將有一個存儲過程返回一組行,然後遍歷該結果,並添加到列表框中。

因此,例如,一個存儲過程是這樣的:

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

-- Insert statements for procedure here 
SELECT FirstName from tblCustomer 
END 

那麼功能更喜歡這個

Dim Customers = dc.CycleCustomers() 

For Each Customer In Customers 
    ListBox2.Items.Add(Customer.FirstName) 
Next 
0

好吧,我做到了通過LINQ to SQL和存儲過程,但現在的工作。下面是按鈕

Dim number As Int32 
    number = 1 
    Dim dc = New DataTestDataContext 

    Do Until number > dc.MaxCustomerId.First.CustomerId 
     Dim Customers = From cust In dc.CycleCustomers(number) _ 
         Where cust.CustomerId = number _ 
         Select cust 

     For Each cust In Customers 
      ListBox2.Items.Add(cust.FirstName) 

     Next 
     number = number + 1 
    Loop 

代碼這裏是存儲過程的代碼

ALTER PROCEDURE CycleCustomers 
-- Add the parameters for the stored procedure here 
@p1 int 

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

-- Insert statements for procedure here 
SELECT CustomerId, FirstName from tblCustomer where CustomerId = @p1 
END 

我做了另一個存儲過程的循環,它選擇最大的ID號,它的代碼是在這裏

ALTER PROCEDURE MaxCustomerId 

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

    SELECT CustomerId 
    FROM tblCustomer 
WHERE CustomerId=(SELECT max(CustomerId) FROM tblCustomer) 

END