Console Application To Display Data From SQL Server
In this post we will learn How to Create a Console Application To Display Data From SQL Server. A console application is a computer program designed to be used via a text-only computer interface, such as a text terminal, the command line interface of some operating systems (Unix, DOS, etc.).
Steps To Create Console Application:
1. Open Visual Studio > Click New Project > Choose Console Application.
2. This will open c# code page, Code for our app is as follows:
Code:
02 | using System.Collections.Generic; |
05 | using System.Threading.Tasks; |
06 | using System.Data.SqlClient; |
09 | namespace ConsoleApplication1 |
13 | static void Main( string [] args) |
15 | SqlConnection con = new SqlConnection( "Data Source=.;Initial Catalog=dev;User ID=sa;Password=sa" ); |
18 | SqlDataAdapter da = new SqlDataAdapter( "SELECT * FROM users" , con); |
19 | DataSet ds = new DataSet(); |
21 | foreach (DataTable dt in ds.Tables) |
23 | Console.WriteLine( "User Details Are Following:" ); |
24 | for ( int curCol = 0; curCol < dt.Columns.Count; curCol++) |
26 | Console.Write(dt.Columns[curCol].ColumnName.Trim() + "\t" ); |
28 | for ( int curRow = 0; curRow < dt.Rows.Count; curRow++) |
30 | for ( int curCol = 0; curCol < dt.Columns.Count; curCol++) |
32 | Console.Write(dt.Rows[curRow][curCol].ToString().Trim() + "\t" ); |