我想要一个循环查询结果的代码,并将它们显示在visual basic的列表框中。到目前为止我所拥有的是存储过程,列表框和按钮。
存储过程即时调用被称为" CycleCustomers"并且sql for that如下:
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个零。我究竟做错了什么?提前致谢。
编辑:这是循环系统功能
<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 :(得分:2)
看看这里:http://msdn.microsoft.com/en-us/library/bb534556(v=vs.110).aspx
你得到的是一个类型为CycleCustomerResult1的ISingleResult,它是一个IEnumerable。你应该用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
答案 1 :(得分: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