To retrieve all database table records with DataRow

Jitendrasoft09
Posted by Jitendrasoft09 under C# category on | Points: 40 | Views : 684
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);

SqlDataAdapter myAdapter = new SqlDataAdapter(
"SELECT * FROM Employees WHERE City='Bangalore'",
myConnection);


DataSet myDataSet = new DataSet();


myAdapter.Fill(myDataSet, "Employees");


DataTable employeeResultTable = myDataSet.Tables["Employees"];


Console.WriteLine("There are {0} DataRows in the DataTable",
employeeResultTable.Rows.Count);

foreach (DataRow row in employeeResultTable.Rows) {

string firstName = (string) row["FirstName"];
string lastName = (string) row["Lastname"];
string city = (string) row["City"];


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


myConnection.Close();

Console.ReadLine();
}
}

Comments or Responses

Login to post response