Posted By

vedgiee on 06/30/10


Tagged

sql StoredProcedure


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tyster


Return Multiple Values Stored Procedure


 / Published in: SQL
 

  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  

You need to login to post a comment.