【问题标题】:Passing stored procedure parameters in c# .net application在 c# .net 应用程序中传递存储过程参数
【发布时间】:2013-06-03 19:56:09
【问题描述】:

我有一个存储过程,我将它连接到我的项目,我想知道如何传递不同的参数类型:

存储过程:

  [dbo].[UploadAssignment]  
          @studentId int    
    , @guid uniqueidentifier  
    , @originalfilename nvarchar(500)   
    , @uploaddate datetime      

在我的项目中:

public virtual IEnumerable<T> GetUploadStudentSubmission<T>(int studentId, .."How should i format the remaining parameters")  
{  
   SqlCommand _command = new SqlCommand("dbo.UploadAssignment");  
   _command.CommandType = CommandType.StoredProcedure;  
   _command.Parameters.Add(new SqlParameter { ParameterName = "studentId",SqlDbType = SqlDbType.Int, Value = sectionId});  
   _command.Parameters.Add(new SqlParameter { ParameterName = "guid", SqlDbType = SqlDbType.Int, Value = guid });  
   _command.Parameters.Add(new SqlParameter { ParameterName = "originalfilename", SqlDbType = SqlDbType.Int, Value = originalfilename });  
   _command.Parameters.Add(new SqlParameter { ParameterName = "uploaddate", SqlDbType = SqlDbType.Int, Value = uploaddate });
} 

【问题讨论】:

  • 我建议将您的 _command.Parameters.Add 更改为 _command.Parameters.AddWithValue("@paramname", variable) 让数据库处理 Sql DataType
  • @DJKRAZE 感谢您的提示,但我实际上担心在方法中传递参数。

标签: c# asp.net-mvc stored-procedures


【解决方案1】:

official documentation 声明:

ParameterName 以@paramname 的形式指定。

因此您需要在参数名称中包含@。除此之外,您只需要像传递给任何其他函数一样传递相关参数,如下所示:

public virtual IEnumerable<T> GetUploadStudentSubmission<T>(
    int studentId, Guid guid, string originalfilename, DateTime uploaddate)
{  
    SqlCommand _command = new SqlCommand("dbo.UploadAssignment");  
    _command.CommandType = CommandType.StoredProcedure;  
    _command.Parameters.Add(new SqlParameter { ParameterName = "@studentId",SqlDbType = SqlDbType.Int, Value = sectionId});  
    _command.Parameters.Add(new SqlParameter { ParameterName = "@guid", SqlDbType = SqlDbType.UniqueIdentifier, Value = guid });  
    _command.Parameters.Add(new SqlParameter { ParameterName = "@originalfilename", SqlDbType = SqlDbType.NVarChar, Value = originalfilename });  
    _command.Parameters.Add(new SqlParameter { ParameterName = "@uploaddate", SqlDbType = SqlDbType.DateTime, Value = uploaddate });
}

【讨论】:

  • 感谢您提供答案,但我主要关心的是方法中传递的参数:'public virtual IEnumerable GetUploadStudentSubmission(int studentId, .."我应该如何格式化剩余参数")'
【解决方案2】:
public virtual IEnumerable<T> GetUploadStudentSubmission<T>(int studentId, Guid guid, string originalfilename, DateTime uploaddate)  
{  
    SqlCommand _command = new SqlCommand("dbo.UploadAssignment");  
    _command.CommandType = CommandType.StoredProcedure;  
    _command.Parameters.AddWithValue("@studentId",sectionId);  
    _command.Parameters.AddWithValue("@guid", guid );  
    _command.Parameters.AddWithValue("@originalfilename",  originalfilename);  
    _command.Parameters.AddWithValue("@uploaddate", uploaddate);
} 

【讨论】:

    【解决方案3】:

    Microsoft 还提供了出色的企业库,除其他内容外,它还包含一个 DataAccess 块并处理参数。

    更多详情请查看此答案https://stackoverflow.com/a/3038469/69433

    【讨论】:

      【解决方案4】:

      您也可以使用以下代码:

      public virtual IEnumerable<T> GetUploadStudentSubmission<T>(int studentId, Guid guid,    string originalfilename, DateTime uploaddate)  
      { 
          var command = Database.GetStoredProcCommand("[dbo].[UploadAssignment]");
          Database.AddInParameter(command, "studentId", DbType.Int32, studentId);
          Database.AddInParameter(command, "guid", DbType.Guid, guid);
          Database.AddInParameter(command, "originalfilename", DbType.String, originalfilename);
          Database.AddInParameter(command, "uploaddate", DbType.DateTime, uploaddate);
      
          var reader = Database.ExecuteReader(command);
          commandText = command.CommandAsSql();
          reader.Close();
      }
      

      【讨论】:

        猜你喜欢
        • 2011-05-01
        • 2016-04-04
        • 2017-08-12
        • 2011-07-22
        • 1970-01-01
        • 2021-07-19
        • 2011-07-21
        • 1970-01-01
        相关资源
        最近更新 更多