The .NET DataSet and OleDbDataAdapter Objects
The .NET DataSet and OleDbDataAdapter Objects
Connection string:
SELECT command:
+nbsp;
void Page_Load(Object sender, EventArgs e)
{
// get connection string from ..\global\connect-strings.ascx user control
string strConnect = ctlConnectStrings.OLEDBConnectionString;
outConnect.InnerText = strConnect; // and display it
// specify the SELECT statement to extract the data
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '1861003%'";
outSelect.InnerText = strSelect; // and display it
// declare a variable to hold a DataSet object
// note that we have to create it outside the Try..Catch block
// as this is a separate block and so is a different scope
DataSet objDataSet = new DataSet();
try
{
// create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect);
// create a new DataAdapter using the connection object and select statement
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSelect, objConnect);
// fill the dataset with data from the DataAdapter object
objDataAdapter.Fill(objDataSet, "Books");
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "* Error while accessing data."
+ objError.Message + "" + objError.Source;
return; // and stop execution
}
// create a DataView object for the Books table in the DataSet
DataView objDataView = new DataView(objDataSet.Tables["Books"]);
// assign the DataView object to the DataGrid control
dgrResult.DataSource = objDataView;
dgrResult.DataBind(); // and bind (display) the data
}