return json c# . net core sql

Asked

Viewed 98 times

1

I’m doing a test with a console application. In it I consult the database and would like to return a file json of the result.

It is possible to return the query json without instantiating the object? how can I do this?

I tried to convert the query return, but it didn’t work:

using (var connection = new SqlConnection("Server=4.4.4.4.;Database=aaaa;user id=aaaa;password=aaaaa"))
        {
            var cmd = new SqlCommand("SELECT * from depposvendas", connection);
            connection.Open();
            SqlDataReader reader = cmd.ExecuteReader();

    //Neste caso estou tentando converter com json.net para json o retorno do SqlDataReader
    //Sem ter que instanciar objeto
            string output = JsonConvert.SerializeObject(reader);
    Console.WriteLine("{0}",output);

        }
  • 2

    Instead of pasting a code image, post the text and format as code. It is easier for your question to be found by the site search.

  • If you don’t want to instantiate an object or use an external library, which is what I believe you meant, just mount the string "manually".

  • I think that before that you still need to learn how to use ADO.NET. Here you begin to explain the ExecuteReader : http://www.macoratti.net/08/11/c_adn_2.htm

  • I put the code in the description

No answers

Browser other questions tagged

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