下面是我存储在 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中声明参数的方式不对?如果是这样,我该如何解决它们?