提供参数时,“过程或函数<procedure>期望参数<param />,未提供”错误

时间:2019-05-31 07:15:53

标签: c# sql-server asp.net-mvc

我正在尝试使用存储过程,以便向表中插入一行。当我将参数添加到SqlParameter对象中,然后执行sqlcommand时,标题中出现错误。该错误将始终抱怨sql过程中声明的 first 参数。切换顺序只会使其抱怨另一个变量。

我正在运行ASP.NET Framework 4.6 MVC 5和SQL Server12。在Web上浏览时,我尝试了几种方法。通过将CommandType = StoredProcedure添加到我从一开始就拥有的命令对象,纠正参数字符串中的拼写错误(我已经对此进行了多次检查)或纠正方法,已经回答了许多问题。用过的。这是我关于该问题的注释:

-尝试了以下具有相同结果的方法(因为它们本质上是相同的东西,并且文档都建议这些方法有效)。香港专业教育学院尝试与对象属性和原始字符串,但它仍然相同。

command.Parameters.Add(new SqlParameter(<param>, <data>));
command.Parameters.Add(<param>, SqlDbType.DateTime).Value=<data>;
command.Parameters.Add(new SqlParameter(<param>, SqlDbType.DateTime)
                {
                    Value = <data>
                });
command.Parameters.AddWithValue(<param>, data.DonationShows);

存储过程通过了我的所有测试后,按预期的方式工作。

-DB连接正常工作,并且我正确地声明了SqlCommand,因为后面的代码正确地调用了另一个存储过程来查询某些行。

这是我的代码:

DateModel.cs

public class DateModel
{
   public int Id { get; set; }
   public string TitleShows { get; set; }
   public DateTime DateShows { get; set; }
   public int DonationShows { get; set; }
   public string FlyerShows { get; set; }
}

DateModel.json(在将参数添加到sqlcommand之前的示例对象内容)

{
    "DateShows" : "2019-05-31-T19:00:00",
    "DonationShows" : 1,
    "FlyerShows" : "https://stackoverflow.com",
    "Id" : 0,
    "TitleShows" : "test"

}

HomeController.cs

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddSchedule(DateModel model)
{
    if(ModelState.IsValid)
        {
            int recordsCreated = 
                    CreateDate(model.Title, model.ShowDate, model.Price, model.FlyerUrl);

                return RedirectToAction("Schedule");
        }
        return View();
}

DateProcessor.cs

public static int CreateDate(string title, DateTime showDate, int price, string url)
{
    DateModel data = new DateModel
    {
         TitleShows = title,
         DateShows = showDate,
         DonationShows = price,
         FlyerShows = url
    };

    string sql = "dbo.InsertUpdateDate";
    return SqlDataAccess.SaveDate(sql, data);
}

SqlDataAccess.cs

public static int SaveDate(string sqlCommand, DateModel data)
{
    using (var conn = new SqlConnection(GetConnectionString()))
    {
         conn.Open();
         SqlCommand command = new SqlCommand(sqlCommand, conn)
         {
             CommandType = StoredProcedure
         };
         command.Parameters.Add("@Action", SqlDbType.NVarChar).Value = "insert";
         command.Parameters.Add("@Titleshows",SqlDbType.NVarChar).Value = data.TitleShows;
         command.Parameters.Add("@Dateshows", SqlDbType.DateTime).Value = data.DateShows;
         command.Parameters.Add("@Donationshows", SqlDbType.Int).Value = data.DonationShows;
          command.Parameters.Add("@Flyershows", SqlDbType.NVarChar).Value = data.FlyerShows;

          return command.ExecuteNonQuery();
    }
}

dbo.InsertUpdateDelete.sql

CREATE PROCEDURE dbo.InsertUpdateDate
    @Action nvarchar(10),
    @Titleshows nvarchar(50),
    @Dateshows datetime,
    @Donationshows int = null,
    @Flyershows nvarchar(max) = null,
    @Id int = null
AS
Begin
    if @Action = 'insert'
        BEGIN TRY
            INSERT INTO dbo.DateShows(TitleShows, DateShows, DonationShows, FlyerShows)
            VALUES      (@Titleshows, @Dateshows, @Donationshows, @Flyershows);
        END TRY
        BEGIN CATCH
            SELECT
                ERROR_NUMBER() as ErrNum,
                ERROR_MESSAGE() as ErrMsg,
                ERROR_LINE() as ErrLine
        END CATCH;
    if @Action = 'update'
        BEGIN TRY
            UPDATE  dbo.DateShows
            SET     TitleShows = @Titleshows,
                    DateShows  = @Dateshows,
                    DonationShows = @Donationshows,
                    FlyerShows = @Flyershows
            WHERE   Id          = @Id;
        END TRY
        BEGIN CATCH
            SELECT
                ERROR_NUMBER() as ErrNum,
                ERROR_MESSAGE() as ErrMsg,
                ERROR_LINE() as ErrLine
        END CATCH;
END

dbo.DateShows.sql

CREATE TABLE [dbo].[DateShows] (
    [Id]            INT            IDENTITY (1000, 101) NOT NULL,
    [TitleShows]    NVARCHAR (50)  NOT NULL,
    [DateShows]     DATETIME       NOT NULL,
    [DonationShows] INT            NULL,
    [FlyerShows]    NVARCHAR (MAX) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

我不太确定问题出在哪里。我得到的一个提示是,当我两次声明相同的参数时,出现以下错误: The variable name <param> has already been declared. Variable names must be unique within a query batch or stored procedure.经过我的尝试,一切进展顺利,直到可以调用ExecuteNonQuery()为止,然后似乎并没有以某种方式将参数传递给存储过程。

0 个答案:

没有答案