Explain how to use the data objects in the previous example
to display the data from a table in an Access database in a
DataGrid control. Ans:
// Create and configure connection:
OleDbConnection c = new OleDbConnection( );
c.ConnectionString =
Provider=Microsoft.Jet.OLEDB.4.0; +
Data Source=e:\\ectserver\\ssmith\\Database\\groceries.mdb;
// Create and configure SelectCommand object.
OleDbCommand sc = c.CreateCommand( );
sc.Commandclass = Commandclass.Text;
sc.CommandText = ddlSelect.SelectedValue;
// Create and configure DataAdapter object.
OleDbDataAdapter da = new OleDbDataAdapter( );
da.SelectCommand = sc;
// Create and populate DataSet object.
DataSet ds = new DataSet( );
int n = da.Fill(ds, Groceries);
// Display data in DataGrid object.
dgGroceries.DataSource = ds;
dgGroceries.DataBind( );