【问题标题】:How to add params in stored procedure in ASP.NET Core 3.0如何在 ASP.NET Core 3.0 的存储过程中添加参数
【发布时间】:2020-08-12 12:43:17
【问题描述】:

我有这个存储过程:

CREATE PROCEDURE AddReserv
    @Place int,
    @StartDate datetime,
    @EndDate datetime
AS
BEGIN
    INSERT INTO [dbo].Reserv(UserId, Place,  StartDate, EndDate)
    VALUES (1, @Place, @StartDate, @EndDate)
END

从 post 方法中的控制器,我想向表中添加一条新记录。我不知道如何将参数传递给存储过程。

我在控制器中的代码:

// POST: api/CreateReserv
[HttpPost]
public void Post(string value)
{
    using (var connection = _db.Database.GetDbConnection())
    {
        connection.Open();

        using (var command = connection.CreateCommand())
        {
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "AddReserv";

            //How to add params?
            /*
                    @Place int
                    @StartDate datetime
                    @EndDate datetime
            */

            command.ExecuteNonQuery();
        }
    }
}

【问题讨论】:

    标签: c# asp.net-core entity-framework-core


    【解决方案1】:

    您可以使用command.Parameters.Add 添加参数,如下所示。

    请确保您已安装NuGetSystem.Data.SqlClient

    using (var command = connection.CreateCommand())
    {
        command.CommandType = System.Data.CommandType.StoredProcedure;
        command.CommandText = "AddReserv";
        
        command.Parameters.Add("@Place", SqlDbType.Int).Value = place;
        command.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = startDate;
        command.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = endDate;
        
        command.ExecuteNonQuery();     
    }
    

    【讨论】:

    【解决方案2】:

    用@Karan 的一些代码更新了我的答案(应该是正确的答案)

    ....
    
      using var connection = _db.Database.GetDbConnection();
      connection.Open();
    
      using var command = new SqlCommand("AddReserv", connection)
      {
        CommandType = CommandType.StoredProcedure
      };
    
      command.Parameters.Add("@Place", SqlDbType.Int).Value = place;
      command.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = startDate;
      command.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = endDate;
      
      command.ExecuteNonQuery();
    

    进一步的改进将是使用代码的异步版本。注意:当您的代码完成后,如果发布 JSON,请不要忘记 [FromBody](有关此内容的更多信息:https://stackoverflow.com/a/63349923/14072498)。

    [HttpPost]
    public async Task<IActionResult> Post(string value)
    {
      await using var connection = _db.Database.GetDbConnection();
      await connection.OpenAsync();
    
      await using var command = new SqlCommand("AddReserv", connection)
      {
        CommandType = CommandType.StoredProcedure
      };
    
      command.Parameters.Add("@Place", SqlDbType.Int).Value = place;
      command.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = startDate;
      command.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = endDate;
    
      await command.ExecuteNonQueryAsync();
    
      ....
    }
    

    不确定 _db 是什么类型,如果

    _db.Database.GetDbConnection()
    

    有一个异步版本,也可以使用它。

    【讨论】:

      猜你喜欢
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      相关资源
      最近更新 更多