CHAPTER 22 ADO.NET PART II: THE DISCONNECTED LAYER
private SqlDataAdapter ordersTableAdapter;
// Form-wide connection string.
private string cnStr = string.Empty;
...
}
The constructor does the grunt work of creating your data-centric member variables and filling the
DataSet. This example assumes you have authored an App.config file that contains the correct
connection string data (and that you have referenced System.Configuration.dll and imported the
System.Configuration namespace), as in this example:
Also note that you include a call to a private helper function, BuildTableRelationship(), as follows:
public MainForm()
{
InitializeComponent();
// Get connection string from *.config file.
cnStr =
ConfigurationManager.ConnectionStrings[
"AutoLotSqlProvider"].ConnectionString;
// Create adapters.
invTableAdapter = new SqlDataAdapter("Select * from Inventory", cnStr);
custTableAdapter = new SqlDataAdapter("Select * from Customers", cnStr);
ordersTableAdapter = new SqlDataAdapter("Select * from Orders", cnStr);
// Autogenerate commands.
sqlCBInventory = new SqlCommandBuilder(invTableAdapter);
sqlCBOrders = new SqlCommandBuilder(ordersTableAdapter);
sqlCBCustomers = new SqlCommandBuilder(custTableAdapter);
// Fill tables in DS.
invTableAdapter.Fill(autoLotDS, "Inventory");
custTableAdapter.Fill(autoLotDS, "Customers");
ordersTableAdapter.Fill(autoLotDS, "Orders");
// Build relations between tables.
BuildTableRelationship();
// Bind to grids.
dataGridViewInventory.DataSource = autoLotDS.Tables["Inventory"];
dataGridViewCustomers.DataSource = autoLotDS.Tables["Customers"];
898