Optionally, you can load the result into a DataSet to work with multiple tables or store?
Short answer: the data in a DataReader. Example: string query = "SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID, Orders.OrderDate " + "FROM Customers " + "INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID"; SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine("Customer: " +…
Explain a bit more
reader["CustomerName"] + ", Order ID: " + reader["OrderID"]); } reader.Close(); connection.Close(); Alternatively, you can use a DataSet to load data from multiple tables, which will maintain the relationships between the tables. Example (Using DataSet): string query = "SELECT * FROM Customers; SELECT * FROM Orders";
Example code
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
DataSet dataSet = new DataSet(); adapter.Fill(dataSet); Here, dataSet.Tables[0] will contain Customers, and dataSet.Tables[1] will contain Orders.
Real-world example (ShopNest)
For large order exports, ShopNest uses SqlDataReader (forward-only, fast). DataSet is heavier and mainly for disconnected editing scenarios.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png