【问题标题】:Addwithvalue causing syntax errorAddwithvalue 导致语法错误
【发布时间】:2013-12-12 04:41:55
【问题描述】:

我有一个存储过程spSchedulerCheck,可以从 SQL Server 正常执行。它采用单个参数@jn。尝试从 VB/ASP.Net 运行它会引发语法错误(我正在捕捉)。

VB 代码:

Public Function ScheduleCheck()
    Dim sText As String
    Using cn As New System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("ProductionDatabaseConnectionString1").ConnectionString)
        cn.Open()
        Dim cmd As New System.Data.SqlClient.SqlCommand("SchedulerCheck", cn)
        cmd.Parameters.AddWithValue("@jn", Trim(Request.QueryString("jn").ToString()))
        Try
            Using reader As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader()
                If reader.Read() Then
                    sText = reader(0).ToString()
                End If
            End Using
        Catch ex As Exception
            Return ex.Message
        End Try

        If Not cn Is Nothing Then
            cn.Close()
        End If
    End Using
        Return sText
End Function

SP:

USE [Production Database 2]
GO

/****** Object:  StoredProcedure [dbo].[SchedulerCheck]    Script Date: 12/11/2013 15:31:48 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[SchedulerCheck] 
    -- Add the parameters for the stored procedure here
    @jn as nvarchar(max)

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
declare @pn nvarchar(10);
set @pn = (select Proposalnumber from tblprj_management where jobnumber = @jn);

select ReturnCode = case
    when not exists (select * from tblprj_equipmentscope where jobnumber = @jn)
        then 'Could not find scope'
    when not exists (select * from tblprj_frcriteria where jobnumber = @jn and 'FR' in (select equipment from tblprj_equipmentscope where jobnumber = @jn)) 
        then 'Could not find FR Criteria entry'
    when not exists (select * from tblprj_wccriteria where jobnumber = @jn and 'WC' in (select equipment from tblprj_equipmentscope where jobnumber = @jn)) 
        then 'Could not find WC Criteria entry'
    when not exists (select * from tblprj_sctcriteria where jobnumber = @jn and 'SCT' in (select equipment from tblprj_equipmentscope where jobnumber = @jn)) 
        then 'Could not find SCT Criteria entry'
    when not exists (select * from tblprj_accriteria where jobnumber = @jn and 'AC' in (select equipment from tblprj_equipmentscope where jobnumber = @jn)) 
        then 'Could not find AC Criteria entry'
    when not exists (select * from tblprj_LFcriteria where jobnumber = @jn and 'LF' in (select equipment from tblprj_equipmentscope where jobnumber = @jn)) 
        then 'Could not find LF Criteria entry'
    when not exists (select * from tblprj_laborest where proposalnumber = @pn)
        and not exists (select * from tblprj_mfglaborest assy where proposalnumber = @pn)
        then 'Could not find labor estimates'
    else 'Success'
    end


END

GO

在我看来,这应该返回 SP 中的一条消息,而不是错误输出。事实上,从 SQL 服务器运行它确实按预期运行。一些故障排除还表明,当我不提供参数时,它会按预期运行,并且只有在我尝试传递参数时才会崩溃。我在做什么导致错误?

编辑:错误是Syntax Error near SchedulerCheck

【问题讨论】:

  • 在底部的编辑中添加。

标签: asp.net sql vb.net


【解决方案1】:

当您调用存储过程时,您需要将属性CommandType 从默认的Text 更改为StoredProcedure

  Dim cmd As New System.Data.SqlClient.SqlCommand("SchedulerCheck", cn)
  cmd.CommandType = CommandType.StoredProcedure

否则 CommandText SchedulerCheck 被解释为普通的 sql 文本 (就像是SELECT/INSERT/UPDATE/DELETE)。当然这会导致语法错误。

AddWithValue 与错误信息无关

【讨论】:

  • 注释掉它会导致它正常运行,没有错误消息。把我扔了。谢谢!编辑:只有当它被赋予参数的默认值时,它才会出现。我的错。
【解决方案2】:

您需要将您的命令的CommandType 设置为StoredProcedure。默认为Text

cmd.CommandType = CommandType.StoredProcedure

【讨论】:

    猜你喜欢
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多