在c#.net应用程序中传递存储过程参数

时间:2013-06-03 19:56:09

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

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

存储过程:

  [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 });
} 

4 个答案:

答案 0 :(得分:2)

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 });
}

答案 1 :(得分:1)

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);
} 

答案 2 :(得分:0)

您还可以使用以下代码:

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();
}

答案 3 :(得分:0)

Microsoft还提供了优秀的企业库,除了其他东西之外还包含DataAccess块并处理参数。

有关详细信息https://stackoverflow.com/a/3038469/69433

,请参阅此答案