过程insertSerial没有提供参数和参数

时间:2012-11-05 10:25:50

标签: c# asp.net stored-procedures

以下是存储在MSSQL服务器中的SP。

USE [testdb]
GO
/****** Object:  StoredProcedure [dbo].[insertSerial]    Script Date: 11/05/2012 16:38:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[insertSerial]
AS

DECLARE @start int
DECLARE @end int
DECLARE @expdate datetime

BEGIN TRAN
WHILE @start < @end
BEGIN
 INSERT tbl_serial VALUES (@start, @expdate)
 SET @start = @start + 1
 END
 COMMIT TRAN    

然后我尝试从我的ASP.NET应用程序中调用SP -

        SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
        SqlCommand cmd = new SqlCommand("insertSerial", sqlconn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@start", txtStart.Text);
        cmd.Parameters.AddWithValue("@end", txtEnd.Text);
        cmd.Parameters.AddWithValue("@expdate", System.DateTime.Now);

        try
        {
            sqlconn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            lblStatus.Text = ex.Message;
        }
        finally
        {
            sqlconn.Close();
        }

然后我收到了Procedure insertSerial has no parameters and arguments were supplied.错误。是因为我在SP中声明参数的方式不正确吗?如果是这样,我该如何修复它们?

3 个答案:

答案 0 :(得分:1)

sp是无效的sql

这是如何声明params

ALTER PROCEDURE [dbo].[foo] @bar INT AS BEGIN

答案 1 :(得分:0)

您的存储过程没有参数,因此您可以在存储过程中declare your parameters between brackets

CREATE PROCEDURE dbo.insertSerial
(
    @start  varchar(1024),
    @end varchar(1024),
    @expdate datetime

)
....

答案 2 :(得分:0)

您在此SP中声明了SQL变量,您需要在此sp中定义参数 喜欢

ALTER PROCEDURE [dbo].[insertSerial]
 @start int
 @end int
 @expdate datetime
AS