Can someone tell me differences between datareader and dataset
Loading
Can someone tell me differences between datareader and dataset
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Srinivasan RamamoorthiPosted Oct 26, 2021, 4:07 AM
Rijwan AnsariPosted Oct 25, 2021, 5:24 PM
Vinitha TPosted Oct 25, 2021, 3:58 PM
Sachin SinghPosted Aug 31, 2021, 6:50 AM
Rajanikant HawaldarPosted Aug 31, 2021, 6:46 AM
Ganesan CPosted Aug 31, 2021, 5:43 AM
DataReader
//opening connection is must
conn.open();
string SQLquery = "SELECT CustomerID, CompanyName FROM dbo.Customers";
SqlCommand cmd = new SqlCommand(SQLquery, conn);
// Call ExecuteReader to return a DataReader
SqlDataReader myReader = cmd.ExecuteReader();
//The Read method of the DataReader object is used to obtain a row from the results of the executed query.
while(myReader.Read())
{
Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
}
//Once you're done with the data reader, call the Close method to close a data reader:
myReader.Close();
//close the connection
conn.close();
DataSet
string SQLquery = "SELECT CustomerID, CompanyName FROM dbo.Customers";
// create DataSet that will hold your Tables/data
DataSet ds = new DataSet("CustomerDataSet");
//Create SqlDataAdapter which acts as bridge to put the data in DataSet,(data is table available by executing your SQL query)
SqlDataAdapter myAdapter = new SqlDataAdapter(SQLquery, conn);
//fill the dataset with the data by some name say "CustomersTable"
myAdapter.Fill(ds,"CustomersTable");
Abhishek ChadhaPosted Aug 31, 2021, 5:29 AM