Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Aug 31, 2021, 2:46 AM EDT
Sachin SinghRank #66 · 26.2K reputationPosted Aug 31, 2021, 2:50 AM EDT
Ganesan CRank #369 · 6.8K reputationPosted Aug 31, 2021, 1:43 AM EDT
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 ChadhaRank #402 · 6.2K reputationPosted Aug 31, 2021, 1:29 AM EDT
Vinitha TRank #8,005 · 230 reputationPosted Oct 25, 2021, 11:58 AM EDT
Rijwan AnsariRank #82 · 24.2K reputationPosted Oct 25, 2021, 1:24 PM EDT
Srinivasan RamamoorthiRank #877 · 2.5K reputationPosted Oct 26, 2021, 12:07 AM EDT