Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 调用存储过程对数组执行计数_Mysql_Vb.net_Stored Procedures - Fatal编程技术网

Mysql 调用存储过程对数组执行计数

Mysql 调用存储过程对数组执行计数,mysql,vb.net,stored-procedures,Mysql,Vb.net,Stored Procedures,我正在从IT\u Cases\u列表中选择一个distinct user,并将其存储在arraystaff()中。 从这个数组中,我将调用一个存储过程来计算此用户参与的案例数,(arraystaff(I)),并循环它直到arraystaff.length-1 但问题是计数不符 Sub getStaff(ByVal month As String) If Not con.State = ConnectionState.Closed Then con.Open() E

我正在从
IT\u Cases\u列表中选择一个
distinct user
,并将其存储在
arraystaff()
中。 从这个数组中,我将调用一个
存储过程
来计算此用户参与的案例数,
(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
,但是我仍然有同样的问题

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

如果我从数据库中获得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)


有人能告诉我我做错了什么吗?

你应该只调用一次
getCases()
,因为它里面每个员工都有一个循环(arraystaff)。另一件事,你能给我们提供更多关于这个问题的信息吗?示例记录,所需输出,以便我们可以为您提供更多帮助:)

更新1

ds=New DataSet
移动到For循环之前,并将
命令对象
传递给
DataAdapter对象

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

为什么不在一个存储过程/查询中完成这一切?不明白为什么需要循环?您的第一个查询对sql注入是开放的。建议像在第二个查询中一样为该查询使用参数。