0
public class Connect
{

    public DataSet Rezultate = new DataSet();
    public void Function(string query, string conexiune)
    {
        using (SqlConnection CONNECT = new SqlConnection(conexiune))
        {

            SqlDataAdapter adaptor = new SqlDataAdapter(query, CONNECT);
            SqlCommand command = new SqlCommand(query, CONNECT);
            try
            {

                command.Connection.Open();
                adaptor.Fill(Rezultate);


            }
            catch (Exception e)
            {
                Console.WriteLine("A aparut o eroare:" + e);
            }

        }
    }
}

class Mainclass
{
    private static void Main()
    {
        Connect a = new Connect();
        a.Function("select * from dbo.test", "Data Source=DESKTOP-S0FG0EC;Initial Catalog=AdventureWorks2016;Integrated Security=True");
        foreach (DataTable table in a.Rezultate.Tables)
        {
            foreach (DataRow row in table.Rows)
            {
                foreach (DataColumn column in table.Columns)
                {
                    Console.Write(a.Rezultate);
                    Console.ReadLine();
                }
            }
        }

    }

}

I want to display the results of the query in Visual Studio console but it gest returns System.Data.DataSet.I tried numerous fixes,none worked.I tried to implement IEnumerable interface also didn't worked.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Daniel
  • 17
  • 1
  • 1
    See https://stackoverflow.com/questions/15547959/print-contents-of-a-datatable –  Dec 10 '18 at 18:32

1 Answers1

3

In your for loop , you are printing whole data set only. so it is supposed to print dataset.

You need to replace following -

    Console.Write(a.Rezultate) 

with

    Console.WriteLine(row[column.ColumnName].ToString());  
Gaurav
  • 623
  • 5
  • 11
  • I tried this fix and It worked somehow.Now,I don't know why,but no matter the query I put in,it always displays "1". – Daniel Dec 10 '18 at 18:48
  • what this query gives you, when you run in DB ?select * from dbo.test – Gaurav Dec 10 '18 at 18:50
  • Update:It works,is there any posibility to show on the console all values exactly as they are presented in a sql query?Basically to show the values as a table:with rows and columns in the console? – Daniel Dec 10 '18 at 18:58
  • In the console , No. But you can create web/windows app for that. or you need to manually try to print something similar. – Gaurav Dec 10 '18 at 19:03