get return from procedure sql c #

3

I need to get the return of a select that comes from a procedure . I've seen about using ExecuteReader , but I'm not getting the data that's coming. procedure is up to date

Since the return of procedure will be something of the type and both varchar .

Item | Name Item

given to | given b
given c | given c

I thought of creating an array of 2 per n lines.

The problem is when it arrives at ExecuteReader to get the values of the item and item name Here's my code

         string[][] retorno = new string[2][];
        string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        using(SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("Sp_SelectContratoGestores"))
            {

                cmd.CommandTimeout = 300;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;
                con.Open();
                while (cmd.ExecuteReader().Read())
                {

                    retorno[0][contador] = cmd.ExecuteReader().ToString();
                    contador++;
                }
                con.Close();
            }
        }
    
asked by anonymous 16.03.2018 / 18:13

2 answers

4

Modify this part of your code:

while (cmd.ExecuteReader().Read())
{
   retorno[0][contador] = cmd.ExecuteReader().ToString();
   contador++;
}

To:

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
   retorno[0][contador] = reader["NOME_DA_SUA_COLUNA"].ToString();
   contador++;
}
    
16.03.2018 / 18:21
1

Example:

string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("Sp_SelectContratoGestores"))
            {

                cmd.CommandTimeout = 300;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (!reader.HasRows)
                    throw new Exception("Não há registros");

                while (reader.Read())
                {
                    string dado0 = reader["dado0"].ToString();
                    int dado1 = int.Parse(reader["dado1"].ToString());

                }
            }
        }
    
16.03.2018 / 18:22