get return from sql c#

Asked

Viewed 1,242 times

3

I need to get the return of one select that comes from a procedure. I saw about using the ExecuteReader, but I’m not getting the data that’s coming in. To procedure is fine

As the return of procedure it’s gonna be something like this and both varchar.

Item | Name Item

given a | given b
given c | given c

I thought of creating a matrix of 2 by n lines.

The problem is when it comes to ExecuteReader to get the values of the item and the item name Here is 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();
            }
        }

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++;
}

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());

                }
            }
        }
  • That’s right, pity that just to select one as solved, I’ll get the guy there who answered first, but thank you, really @Diegosantos

  • 1

    When I saw him I had already answered hehehe. But ta tranquil. The important thing is to kill the doubts! No competition :)

  • By the way, is this way of writing the 2 by n multidimensional array correct? (2 columns n rows)

  • @gabrielfalieri, which array? The one from Reader?

  • The string that takes the value of the Reader

  • But it is not multidimensional... Note that I passed the content inside a string. If I passed a number, it would be the index of the resultset.

  • I already know what I will do, I will take this dataReader, play for a dataset, with this dataset I take his Count and with it I can create an array of lines the size of Count

  • Forget it, it’ll be all right

  • Dude, I don’t think it’s gonna work out not even kkkkkkkkkkkkkkkkkkkkkk Take each attribute the way I showed it. If not, only an ORM will help you

  • Oh that’s... I have to play this returned data to become an excel later

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.