SQL Server过程指定了太多参数

时间:2018-01-22 08:22:03

标签: asp.net sql-server

我收到错误

  

过程或函数必须指定许多参数

但有时只是。它是一个REST API,手机调用此函数来验证用户。今天它是onli 1-2手机,他们每隔一分钟拨打一次电话。

大部分时间效果很好,但有时我会收到此错误代码。

为了看到什么是错的,我做了一个小循环,将参数收集到一个字符串中。有时它们是两个@GUID@Datum,有时会重复。

这是VB代码:

Public Shared Sub validatePhone(secretKey As String, ByRef _RS As phoneData, Fnr As Integer)
    Dim connStr As String = System.Configuration.ConfigurationManager.AppSettings("ConnStringSQL")
    Dim Conn As SqlConnection = New System.Data.SqlClient.SqlConnection(connStr)
    Dim _theString As New System.Text.StringBuilder

    Try
        _RS.message = ""
        _RS.status = True
        Dim Status As Integer = 0
        Dim _GUID As Guid = New Guid(secretKey)

        Try
            Conn.Open()
        Catch ex As Exception
        End Try

        cmd.Connection = Conn
        Apt.SelectCommand = cmd
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "API2017_checkPhone"

        cmd.Parameters.Clear()
        cmd.Parameters.Add("@GUID", SqlDbType.UniqueIdentifier).Value = _GUID
        cmd.Parameters.Add("@Datum", SqlDbType.SmallDateTime).Value = Now

        For Each item As SqlParameter In cmd.Parameters
            _theString.Append(item.ParameterName & ";")
        Next

        Apt.Fill(ds, "DataCheckPhone")

        If ds.Tables("DataCheckPhone").Rows.Count > 0 Then
            With ds.Tables("DataCheckPhone").Rows(0)
                Status = .Item("spStatus")

                If Status = 0 Then
                    _RS.Namn = .Item("Namn")
                    _RS.SalesID = .Item("SalesID")
                    _RS.Anlaggning = .Item("Anlaggning")
                    _RS.Anlnr = .Item("Anlnr")
                    Funktions.URLEncodeStr(_RS.Namn)
                    Funktions.URLEncodeStr(_RS.Anlaggning)
                End If
            End With
        Else
            Dim _Lnum As Integer
            Funktions.Logg(Fnr & ": " & "Fatal error", 999, _Lnum, 0)
            _RS.message = "Error 999:" & _Lnum.ToString
            Return
        End If

    Catch ex As Exception
        _RS.status = False
        _RS.Anlaggning = Nothing
        _RS.Anlnr = 0
        _RS.Namn = Nothing
        _RS.SalesID = Nothing
        Dim _Lnum As Integer
        Funktions.Logg(Fnr & ": " & ex.Message & "{" & _theString.ToString & "}", 999, _Lnum, 0)
        _RS.message = "Error 999:" & _Lnum.ToString

    Finally
        ds.Tables.Clear()

        Try
            ds.Tables("DataCheckPhone").Dispose()
        Catch ex As Exception
        End Try
    End Try

    Try
        Conn.Close()
        Conn.Dispose()
    Catch ex As Exception
    End Try
End Sub

这是存储过程:

ALTER PROCEDURE [dbo].[API2017_checkPhone]
    @GUID as uniqueidentifier,
    @Datum as smalldatetime
AS
    DECLARE @Status AS INT
    DECLARE @Anlaggning AS NVARCHAR(50)
    DECLARE @Namn AS NVARCHAR(50)
    DECLARE @Anlnr AS INT
    DECLARE @SalesID AS uniqueidentifier
    DECLARE @LockedSalesman AS BIT
    DECLARE @LockedAnlaggning AS BIT

    SET @Status = 0

    IF EXISTS (SELECT * FROM KK2017_connectedPhones WHERE [guid] = @guid)
    BEGIN
        SET @status = 0
        SET @salesID = (SELECT salesID FROM KK2017_connectedPhones 
                        WHERE [guid] = @guid)
        SET @Anlnr = (SELECT anlnr FROM KK2017_Säljare WHERE salesID = @salesID)
        SET @Namn = (SELECT Namn FROM KK2017_Säljare WHERE salesID = @salesID)
        SET @Anlaggning = (SELECT namn FROM KK2017_Anlaggning WHERE anlnr = @Anlnr)
        SET @LockedSalesman = (SELECT locked FROM KK2017_Säljare WHERE salesID = @salesID)

        UPDATE KK2017_Säljare 
        SET inloggad = @Datum 
        WHERE salesID = @SalesID

        IF @LockedSalesman = 1 
        BEGIN
            SET @Status = 2
        END

        SET @LockedAnlaggning = (SELECT locked FROM KK2017_Anlaggning 
                                 WHERE AnlNr = @Anlnr)

        IF @LockedAnlaggning = 1
        BEGIN
            SET @status = 3
        END
    END
    ELSE
       SET @status = 1

    SELECT 
        @Status AS spStatus, 
        @Anlaggning AS Anlaggning, 
        @anlnr AS anlnr, 
        @Namn AS namn, 
        @SalesID AS salesID

我一定做错了但看不到。

如果有人提出建议,我将不胜感激。

/克拉斯

1 个答案:

答案 0 :(得分:1)

您遇到了线程问题,因为您已将cmd定义为shared,这意味着只有一个实例为所有传入请求提供服务。有时两个线程正在同时添加参数,两个线程最终都有四个,因此错误。

此处的标准模式将声明cmd作为局部变量,并在每次调用函数时实例化一个新实例。对此的开销可以忽略不计。

此外,避免多线程应用程序(例如Web服务)中的shared变量。对于所有用户和所有线程,只有一个变量副本,99%的时间不是您真正想要的。对于其他1%,通常您使用应用程序变量或HttpCache。在这种情况下,您应该使用局部变量。