-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.

3
  • Does this answer your question? Print Contents Of A DataTable Commented Apr 28, 2020 at 1: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 Commented Apr 28, 2020 at 1: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. Commented Apr 28, 2020 at 1:49

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.