How To Read Data From Database Using ADO.NET

Introduction 

In this blog, you will learn how we can read the data from data using ADO.NET.

Below code snippet will help you understand how we will use ADO.Net to retrieve the data from the database.

using System.Data.SqlClient;

string ConString = @"Server=localhost\SQLEXPRESS;Database=Test1;Trusted_Connection=True;";
SqlConnection con = new SqlConnection(ConString);
string SqlQuery = "Select * from UsersBackUp";
con.Open();
try
{
	SqlCommand sqlCommand = new SqlCommand(SqlQuery,con);
	SqlDataReader reader = sqlCommand.ExecuteReader();
	while (reader.Read())
	{
		Console.WriteLine(reader[0].ToString() + "\n" + 
			reader[1].ToString() + "\n" + reader[2].ToString()+"\n"
			+reader[3].ToString());
	}
}
catch (Exception)
{
	throw;
}
finally { 
	con.Close();
	Console.ReadKey();
}

Program Output

Remove The Duplication From The String Using HashSet