出于某种原因,我的存储过程都执行了两次!我有一个静态函数,它运行一个给定名称和参数的sp,并填充一个数据表。
Public Shared Function RunSP(ByVal spName As String, ByRef spParams As Dictionary(Of String, String), ByRef pDataTable As DataTable) As Integer
Dim cmd As New SqlCommand
Dim lAdapter As SqlDataAdapter
Try
cmd.Connection = New SqlConnection(ConnectionString)
cmd.Connection.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = spName
For Each item As KeyValuePair(Of String, String) In spParams
cmd.Parameters.AddWithValue(item.Key, item.Value)
Next
cmd.ExecuteNonQuery()
If Not pDataTable Is Nothing Then
lAdapter = New SqlDataAdapter(cmd)
lAdapter.Fill(pDataTable)
End If
RunSP = 0
Catch ex As Exception
If (Not cmd Is Nothing) Then
cmd.Dispose()
End If
RunSP = -1
End Try
End Function
代码有问题吗?我已经用调试器进行了检查,并且相应的SP肯定只被调用一次,也就是说,对于特定的插入函数,这个函数只运行一次,但是有两件事被插入到表中。