2013-06-03 115 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(); 
} 
相關問題