string connectionString = "Data Source=serverName;Initial Catalog=databaseName;Integrated Security=true"; string query = "SELECT * FROM Customers"; DataTable dt = new DataTable(); using(SqlConnection connection = new SqlConnection(connectionString)) { SqlDataAdapter adapter = new SqlDataAdapter(query, connection); adapter.Fill(dt); }
string connectionString = "Data Source=serverName;Initial Catalog=databaseName;Integrated Security=true"; string query = "SELECT Customers.CustomerName, Orders.OrderDate FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID"; DataSet ds = new DataSet(); using(SqlConnection connection = new SqlConnection(connectionString)) { SqlDataAdapter adapter = new SqlDataAdapter(query, connection); adapter.Fill(ds, "CustomersAndOrders"); }In this example, we use a JOIN statement to retrieve data from two tables (Customers and Orders) and store the results in a DataSet object with a name of "CustomersAndOrders". The System.Data.SqlClient namespace is part of the .NET Framework Class Library, which is included in the Microsoft .NET Framework package.