2013-06-03 68 views
1

我有一个存储过程,我把它连接到我的项目,我想知道我可以通过不同的参数类型:在C#.NET应用程序传递存储过程的参数

存储过程:

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

我建议改变你的'_command.Parameters.Add'到'_command.Parameters.AddWithValue( “@ PARAMNAME”,变量)'让数据库处理SQL数据类型 – MethodMan

+0

@DJKRAZE感谢您的提示,但我实际上担心传递方法中的参数。 – Masriyah

回答

2

official documentation状态:

参数名称的格式为@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 }); 
} 
+0

感谢您提供一个答案,但我主要关心的是在参数传递的方法:'公共虚拟IEnumerable GetUploadStudentSubmission (int studentId,..“我应该如何格式化其余参数”)' – Masriyah

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); 
} 
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(); 
} 
相关问题