【发布时间】:2018-08-03 01:02:07
【问题描述】:
好的,首先让我为提出这个看似多余的问题而道歉。我正在尝试优化存储过程以及使用它的代码。我已经从代码和存储过程中获取了所有参数,并将它们并排放在 Excel 中,以计算每个参数的数量。两者都有23个。我认为有 1 个输出参数可能会导致问题。我不确定如何使用它。
当我运行代码时出现错误
System.Data.SqlClient.SqlException:过程或函数 spr_SelectClaims 指定的参数过多。
存储过程参数:
@BID int = NULL,
@IndID int = NULL,
@RecDateStart datetime = NULL,
@RecDateEnd datetime = NULL,
@ServiceTypeID int = NULL,
@Billed bit = NULL,
@Paid bit = NULL,
@Approved bit = NULL,
@Void bit = 0,
@LocationID int = NULL,
@BillingAgingDate datetime = NULL,
@PaymentAgingDate datetime = NULL,
@ClaimID int = NULL,
@Eligible bit = NULL,
@BillingTypeID int = NULL,
@ClaimDetailID int = NULL,
@SortExpression varchar(25) = NULL,
@ProvidedBy int = NULL,
@Billable varchar(10) = NULL,
@ExpenseOnly varchar(10) = NULL,
@PageIndex INT = NULL,
@PageSize INT = NULL,
@RecordCount INT OUTPUT
VB.NET 代码
Dim helper As New DataBaseHelper(StoredProcedureName)
Dim _param As SqlParameter() = {
New SqlParameter("@RecordCount",SqlDbType.Int),
New SqlParameter("@BID", IIf(Me.BID Is Nothing, DBNull.Value, Me.BID)),
New SqlParameter("@IndID", IIf(Me.IndID Is Nothing, DBNull.Value, Me.IndID)),
New SqlParameter("@RecDateStart", IIf(Me.RecDate Is Nothing, DBNull.Value, Me.RecDate)),
New SqlParameter("@RecDateEnd", IIf(Me.RecDateEnd Is Nothing, DBNull.Value, Me.RecDateEnd)),
New SqlParameter("@ServiceTypeID", IIf(Me.ServiceTypeID Is Nothing, DBNull.Value, Me.ServiceTypeID)),
New SqlParameter("@Billed", IIf(Me.Billed Is Nothing, DBNull.Value, Me.Billed)),
New SqlParameter("@Paid", IIf(Me.Paid Is Nothing, DBNull.Value, Me.Paid)),
New SqlParameter("@Approved", IIf(Me.Approved Is Nothing, DBNull.Value, Me.Approved)),
New SqlParameter("@Void", IIf(Me.Void Is Nothing, DBNull.Value, Me.Void)),
New SqlParameter("@LocationID", IIf(Me.LocationID Is Nothing, DBNull.Value, Me.LocationID)),
New SqlParameter("@BillingAgingDate", IIf(Me.BillingAgingDate Is Nothing, DBNull.Value, Me.BillingAgingDate)),
New SqlParameter("@PaymentAgingDate", IIf(Me.PaymentAgingDate Is Nothing, DBNull.Value, Me.PaymentAgingDate)),
New SqlParameter("@Eligible", IIf(Me.Eligible Is Nothing, DBNull.Value, Me.Eligible)),
New SqlParameter("@BillingTypeID", IIf(Me.BillingTypeID Is Nothing, DBNull.Value, Me.BillingTypeID)),
New SqlParameter("@ClaimID", IIf(Me.ClaimID Is Nothing, DBNull.Value, Me.ClaimID)),
New SqlParameter("@ClaimDetailID", IIf(Me.ClaimDetailID Is Nothing, DBNull.Value, Me.ClaimDetailID)),
New SqlParameter("@SortExpression", IIf(sortExpresion Is Nothing, DBNull.Value, sortExpresion)),
New SqlParameter("@ProvidedBy", IIf(Me.ProvidedBy Is Nothing, DBNull.Value, Me.ProvidedBy)),
New SqlParameter("@Billable", IIf(billableonly Is Nothing, DBNull.Value, billableonly)),
New SqlParameter("@ExpenseOnly", IIf(expenseonly Is Nothing, DBNull.Value, expenseonly)),
New SqlParameter("@PageIndex", Me.PageIndex),
New SqlParameter("@PageSize", Me.PageSize)}
_param(0).Direction =ParameterDirection.Output
Dim dr As IDataReader = helper.RunDataReader(_param)
执行代码
Public Function RunDataReader(ByVal parameters As SqlParameter()) As IDataReader
Dim dr As IDataReader
Dim sqlCommand As String = MyBase.StoredProcedureName
Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
dbCommand.CommandTimeout = 160
If Not (parameters Is Nothing) Then
For Each param As SqlParameter In parameters
db.AddInParameter(dbCommand, param.ParameterName, param.DbType, param.Value)
Next param
End If
'HttpContext.Current.Response.Write(parameters.Length)
dr = db.ExecuteReader(dbCommand)
Return dr
End Function
【问题讨论】:
-
您已经向我们展示了在哪里创建了
SqlParameter对象数组。您还没有向我们展示您对该数组的实际操作。据我们所知,您将每个元素添加到命令中 100 次。 -
有什么帮助?这里有太多代码要放置。在 Dim dr As IDataReader = helper.RunDataReader(_param) 之后有一个 While dataReader.Read() 调用。 Dim dr As IDataReader = helper.RunDataReader(_param) 发生错误
-
顺便说一句,在这个时代你不应该使用
IIf。 VB 使用If运算符已有近十年了。你应该使用它。 -
我同意。我继承了这段代码并慢慢尝试优化它并使其加速。这可以追溯到 .net 2 天。
-
我建议您在
ExecuteReader行上放置一个断点并在该点检查命令对象。根据所提供的信息,我们只能得出结论,正在调用的存储过程的命令参数太多,或者该存储过程调用另一个具有太多参数的存储过程或数据库函数。我们不能从这里真正测试它。但我会按照上面的描述做。也许您实际上并没有执行您认为的存储过程。
标签: asp.net sql-server vb.net webforms