Friday, March 8, 2013

How to call and pass parameter to a MS SQL Server Stored procedure


Refer the below code to call and pass parameter to a MS SQL Server Stored procedure. Replace the YOUR CONNECTION STRING text with the connection string of your database.
 Here UpdateIssueStatus_SP is the Stored Procedure name.
  1. SqlParameter param;
  2. SqlCommand cmd;
  3. string connStr = "YOUR CONNECTION STRING";
  4.  
  5. public void UpdateIssueStatus(int issueNo, char status)
  6. {
  7. try
  8. {
  9. SqlConnection conn = new SqlConnection(connStr);
  10. conn.Open();
  11. cmd = new SqlCommand("UpdateIssueStatus_SP", conn);
  12. cmd.CommandType = System.Data.CommandType.StoredProcedure;
  13. param = new SqlParameter("@IssueNo", SqlDbType.Int);
  14. param.Direction = ParameterDirection.Input;
  15. param.Value = issueNo;
  16. cmd.Parameters.Add(param);
  17. param = new SqlParameter("@ApprovalStatus", SqlDbType.Char);
  18. param.Direction = ParameterDirection.Input;
  19. param.Value = status;
  20. cmd.Parameters.Add(param);
  21. cmd.ExecuteNonQuery();
  22. conn.Close();
  23. }
  24. catch (Exception ex)
  25. {
  26. }
  27. }

No comments:

Post a Comment