Ad Code

Responsive Advertisement

Console App Task 23: How to print a table name along with its contents using Console.WriteLine in C#

Console App Task 23: How to print a table name along with its contents using Console.WriteLine in C# 

Yes, it’s possible to print a table name along with its contents using `Console.WriteLine`, but you need to ensure that you have the table name as a string and the contents in a format that can be printed. If `tbl` represents the table or its data, you need to convert it to a string format suitable for printing.


Here’s a basic example:

1. Define the Table and Data

   Assuming you have a table with some data, you might want to print the table name and its contents.

   using System;
   using System.Data;
   using System.Data.SqlClient;
   using System.Configuration;

   namespace PrintTableNameAndData
   {
       class Program
       {
           static void Main(string[] args)
           {
               string tableName = "YourTableName"; // Replace with your table name
               DataTable dataTable = GetDataFromTable(tableName);
               
               // Print table name
               Console.WriteLine("Table Name: " + tableName);

               // Print table data
               PrintTableData(dataTable);
           }

           private static DataTable GetDataFromTable(string tableName)
           {
               string connectionString = ConfigurationManager.ConnectionStrings["BajajTMSResultMasterEntities"].ConnectionString;
               string query = $"SELECT * FROM {tableName}";

               using (SqlConnection connection = new SqlConnection(connectionString))
               {
                   SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
                   DataTable dataTable = new DataTable();
                   adapter.Fill(dataTable);
                   return dataTable;
               }
           }

           private static void PrintTableData(DataTable dataTable)
           {
               // Print column names
               foreach (DataColumn column in dataTable.Columns)
               {
                   Console.Write(column.ColumnName + "\t");
               }
               Console.WriteLine();

               // Print rows
               foreach (DataRow row in dataTable.Rows)
               {
                   foreach (var item in row.ItemArray)
                   {
                       Console.Write(item.ToString() + "\t");
                   }
                   Console.WriteLine();
               }
           }
       }
   }

2. Explanation

   - Table Name: The table name is stored in the `tableName` variable and printed using `Console.WriteLine("Table Name: " + tableName);`.

   - Data Table: Data from the table is retrieved and stored in a `DataTable` object using `GetDataFromTable`.

   - Print Table Data: The `PrintTableData` method prints the column names and row data.

Important Considerations

- SQL Injection: Be cautious with SQL queries to avoid SQL injection. Ensure that `tableName` is a valid table name and not user input directly in a real application.
- Performance: For large datasets, consider paging or limiting results to avoid performance issues.

By using the above approach, you can print both the table name and its contents to the console. If you like comment and share. 🚀

Post a Comment

0 Comments

Ad Code

Responsive Advertisement