Return Multiple Values Stored Procedure


/ Published in: SQL
Save to your folder(s)



Copy this code and paste it in your HTML
  1. ######### SQL #########################################
  2.  
  3. CREATE PROCEDURE dbo.getTwoValues
  4. (
  5. @param1 INT
  6. )
  7.  
  8. AS
  9.  
  10. SELECT returnval1, returnval2 FROM tblWhatever WHERE param1 = @param1
  11.  
  12. ########## C# #########################################
  13.  
  14. string connString = ConnectionManager.ConnectionString["ConnString"].ToString();
  15.  
  16. USING(SqlConnection conn = NEW SqlConnection(connString))
  17. {
  18. SqlCommand getTwoValues = NEW SqlCommand("[getTwoValues]", conn);
  19. getTwoValues.CommandType = ...StoredProcedure;
  20. getTwoValues.Parameters.AddWithValue("@param1", 1);
  21.  
  22. conn.Open();
  23. SqlDataReader reader = getTwoValues.ExecuteReader();
  24.  
  25. // Repeat FOR ALL returned ROWS
  26. while(reader.Read())
  27. {
  28. string firstValue = reader.GetString(reader.GetOrdinal("returnval1"));
  29. INT secondValue = reader.GetInt32(readder.GetOrdinal("returnval2"));
  30.  
  31. // Do stuff WITH VALUES
  32. }
  33. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.