none
Read Procedure with Two queries in one sql connection RRS feed

  • Question

  • Hi. Everybody Im new in these forums and my english is bad.. but I have this question.!

    I have this procedure:

    ALTER PROCEDURE [dbo].[X]

          @Tipo int

    AS

    BEGIN

          if(@Tipo = '1')

                BEGIN

                      SELECT    

                            IdDependencia,

                            NombreDependencia

                      FROM        

                            Dependencia

                      SELECT    

                            IdRol,

                            NombreRol,

                            EstadoRol

                      FROM        

                            Rol

                END

    END

     

    When I execute this procedure in SqlManagement the result are two tables, but i dont know read before procedure in C# Vs2010! with one SqlData Reader?

    Im try:

     

    SqlConnection Connection = new SqlConnection(@"Data Source=MARIA7-PC\SQLEXPRESS;Initial Catalog=DbHelpDesk;Integrated Security=SSPI;");

          SqlCommand Command = new SqlCommand("X", Connection);

          Command.CommandType = System.Data.CommandType.StoredProcedure;

          Command.Parameters.AddWithValue("@Tipo", "1");

          SqlDataReader Reader;

     

          try

          {

            using (Connection)

            {

              Connection.Open();

              Reader = Command.ExecuteReader();

              Console.WriteLine("FieldCount Columns: {0}", Reader.FieldCount);

            }

          }

          catch (SqlException ex)

          {

            Console.WriteLine("Error: {0}", ex.Message);

          }

          Console.ReadLine();

     

    Friday, March 4, 2011 9:39 PM

Answers

All replies

  • Hello JUAN CAMILO CARO,

     

    Welcome to the MSDN Forum and thanks for posting here.

    As you description, did you have some error messages or exceptions?could you please show them to us? With the infomation of error messages or exceptions, we could try to solve your issue better and quicker.

    Thank you for your understanding and support!

    I'm looking forward to hearing from you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 7, 2011 9:50 AM
    Moderator
  • You need to use NextResult method of SqlDataReader to move to the next returned result. Here is an example how to do this

    http://support.microsoft.com/kb/311274


    Val Mazur (MVP) http://www.xporttools.net
    Monday, March 7, 2011 10:57 AM
    Moderator