-2

I'm trying to run an SQL query for SQL Server from the Console Application, but I can't manipulate or show this data on the console.

I tried this:

static void Main(string[] args)
{
    string conexao = @"Integrated Security=SSPI;Persist Security Info=False;" +
         "Initial Catalog=Linhas;Data Source=HP\SQLEXPRESS";
    SqlConnection cn = new SqlConnection(conexao);
    SqlDataAdapter Da_fun = new SqlDataAdapter(
       @"select top 1 Name as '[NAME_USER]',Adress as '[ADRESS_USER]' " +
              "from TB_User order by ID_User asc", cn);
    DataTable Tb_fun = new DataTable();
    Da_fun.Fill(Tb_fun);
    Console.WriteLine(Tb_fun);

    Console.ReadKey();
}

Prints something like "System.Data.DataTable" instead of beautifully formatted multicolumn layout.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Dev OV
  • 19
  • 6
  • Does this answer your question? [Print Contents Of A DataTable](https://stackoverflow.com/questions/15547959/print-contents-of-a-datatable) – Adam Apr 28 '20 at 01:39
  • 1
    If it's a a console application, it's not ASP.NET Core, is it? Please don't add unrelated tags to your questions. If you're unsure of the difference, please read [.NET Core vs ASP.NET Core](https://stackoverflow.com/questions/44136118/net-core-vs-asp-net-core) – ProgrammingLlama Apr 28 '20 at 01:46
  • 2
    The shown code *does* "run an SQL query for SQL Server from the Console Application", so that's no the issue (unless there is an error, which should be reported) and thus should *not* be the title. Then 1) there is no code related to manipulation and 2) the real issue appears to be that DataTable.ToString does not conveniently return a formatted table (it will still print something, which should be a clue) .. work to isolate specific issues and provide relevant observations when formulating questions. – user2864740 Apr 28 '20 at 01:49

1 Answers1

2

Datatable is collection of rows and columns Hence, you need to iterate over rows and then need to print each cell value. See below example:

static DataTable GetTable()
    {
        DataTable table = new DataTable(); // New data table.
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));
        table.Rows.Add(15, "Abilify", "Jacob", DateTime.Now);
        table.Rows.Add(40, "Accupril", "Emma", DateTime.Now);
        table.Rows.Add(40, "Accutane", "Michael", DateTime.Now);
        table.Rows.Add(20, "Aciphex", "Ethan", DateTime.Now);
        table.Rows.Add(45, "Actos", "Emily", DateTime.Now);
        return table; // Return reference.
    }
    private static void Main(string[] args)
    {
        DataTable table = GetTable();
        foreach (DataRow row in table.Rows)
        {
            Console.WriteLine("--- Row ---");
            foreach (var item in row.ItemArray)
            {
                Console.Write("Item: "); // Print label.
                Console.WriteLine(item);
            }
        }
        Console.Read(); // Pause.

    }

//Output
--- Row ---
Item: 15
Item: Abilify
Item: Jacob
Item: 4/28/2020 7:18:18 AM
--- Row ---
Item: 40
Item: Accupril
Item: Emma
Item: 4/28/2020 7:18:18 AM
--- Row ---
Item: 40
Item: Accutane
Item: Michael
Item: 4/28/2020 7:18:18 AM
--- Row ---
Item: 20
Item: Aciphex
Item: Ethan
Item: 4/28/2020 7:18:18 AM
--- Row ---
Item: 45
Item: Actos
Item: Emily
Item: 4/28/2020 7:18:18 AM
Jayesh Tanna
  • 398
  • 5
  • 17