To read all data int table from database with sqldatareader

Jitendrasoft09
Posted by Jitendrasoft09 under C# category on | Points: 40 | Views : 773
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

class jkexample {

static void Main(string[] args) {

ConnectionStringSettings connStringSettings
= ConfigurationManager.ConnectionStrings["NorthwindConnection"];


SqlConnection myConnection = new SqlConnection(connStringSettings.ConnectionString);


SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "SELECT * FROM Employees WHERE City='Bangalore'";


myConnection.Open();


SqlDataReader myReader = myCommand.ExecuteReader();


while (myReader.Read()) {

string firstName = (string) myReader["FirstName"];
string lastName = (string) myReader["LastName"];
string city = (string) myReader["City"];


Console.WriteLine("Name: {0} {1}, City: {2}",
firstName, lastName, city);
}


myConnection.Close();


Console.ReadLine();
}
}

Comments or Responses

Login to post response