0

我正在distinct user从中选择一个IT_Cases_List并将其存储在一个arraystaff(). 从这个数组中,我将调用 aStored Procedure 来计算这个用户参与的案例数(arraystaff(i))并循环它直到 arraystaff.length-1

但问题是计数不相符。

Sub getStaff(ByVal month As String)
    If Not con.State = ConnectionState.Closed Then
        con.Open()
    End If
    Dim s As String = "select distinct Attended_by from IT_Cases_List where month(Resolution_date) ='" & month & "' "
    s = s & "And Year(Resolution_date) ='" & ddyear.SelectedValue & "' and Attended_by is not null "
    cmd = New SqlCommand(s, con)
    da = New SqlDataAdapter
    ds = New DataSet
    da.SelectCommand = cmd
    da.Fill(ds)
    If ds.Tables(0).Rows.Count > 0 Then
        staffcount = ds.Tables(0).Rows.Count
        ReDim arrstaff(staffcount - 1)
        For Me.i = 0 To staffcount - 1
            arrstaff(i) = ds.Tables(0).Rows(i).Item("Attended_by")
        Next
            getCases()
     End If
End Sub

Sub getCases()
    If con.State = ConnectionState.Closed Then
        con.Open()
    End If

    ReDim arrdata(arrstaff.Length - 1, 0)
    For Me.i = 0 To arrstaff.Length - 1
        cmd = New SqlCommand("get_cases", con)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@ename", SqlDbType.VarChar).Value = arrstaff(i)
        cmd.Parameters.Add("@Yr", SqlDbType.VarChar).Value = ddyear.SelectedValue
        cmd.Parameters.Add("month", SqlDbType.VarChar).Value = m

        cmd.ExecuteNonQuery()
        da = New SqlDataAdapter()
        da.SelectCommand = cmd
        ds = New DataSet
        da.Fill(ds)

        If Not IsDBNull(ds.Tables(0).Rows(i).Item("NoCase")) Then
            arrdata(i, 0) = ds.Tables(0).Rows(i).Item("NoCase")
        End If
    Next

    cmd = New SqlCommand("delete from cases_Temp", con)
    cmd.ExecuteNonQuery()
    con.Close()
End Sub

这是我的存储过程

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[get_cases](
@Ename  varchar(100),
@Yr varchar(50),
@month varchar(30))

 AS
BEGIN
declare @NoCase as int

select @NoCase=COUNT(*)
from IT_Cases_List
where Attended_by= @Ename and month(Resolution_date) =@month and 
Year(Resolution_date)=@Yr and Attended_by is not null 


insert into cases_temp(Ename,NoCase)
values(@Ename,@NoCase)

select * from cases_Temp

end

我不知道我做错了什么。任何帮助都感激不尽。

更新

好的,我只打过一次电话,getcases但我仍然遇到同样的问题。

这是我运行程序时得到的:

http://imgur.com/dkhmU

如果我从数据库中得到 Count(*),我应该得到的病例总数是 132,而我从程序中得到的病例总数是 157 (7+7+20+20+49+49+5)

如果我从 sql 运行查询,这就是我应该得到的 在此处输入图像描述

我注意到计数被重复 (7,7,20,20,49,49,5) 而不是 (7,20,49,5,10,27,13)

谁能告诉我我做错了什么?

4

1 回答 1

1

您应该只调用getCases()一次,因为在它内部每个员工 ( arraystaff ) 都有一个循环。另一件事,您能否向我们提供有关该问题的更多信息?例如。样本记录,所需的输出,以便我们可以为您提供更多帮助:)

更新 1

ds = New DataSet在 For 循环之前移动,并将传递给.Command ObjectDataAdapter Object

Sub getCases()
    If con.State = ConnectionState.Closed Then
        con.Open()
    End If

    ReDim arrdata(arrstaff.Length - 1, 0)

    ds = New DataSet
    For Me.i = 0 To arrstaff.Length - 1

        cmd = New SqlCommand("get_cases", con)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@ename", SqlDbType.VarChar).Value = arrstaff(i)
        cmd.Parameters.Add("@Yr", SqlDbType.VarChar).Value = ddyear.SelectedValue
        cmd.Parameters.Add("month", SqlDbType.VarChar).Value = m

        da = New SqlDataAdapter(cmd)
        da.Fill(ds)

        If Not IsDBNull(ds.Tables(0).Rows(i).Item("NoCase")) Then
            arrdata(i, 0) = ds.Tables(0).Rows(i).Item("NoCase")
        End If

    Next

    cmd = New SqlCommand("delete from cases_Temp", con)
    cmd.ExecuteNonQuery()
    con.Close()
End Sub
于 2012-05-21T05:13:54.020 回答