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 BookList data string strSelectBooks = "SELECT * FROM BookList WHERE ISBN LIKE '18610033%'"; outSelectBooks.InnerText = strSelectBooks; // and display it
// specify the SELECT statement to extract the BookAuthor data string strSelectAuthors = "SELECT * FROM BookAuthors WHERE ISBN LIKE '18610033%'"; outSelectAuthors.InnerText = strSelectAuthors; // and display it
// Declare a variable to hold a dataset object dataset objDataSet = new dataset ();
Try {// create a new connection Object Using the connection = new OLEDBCONNECTION (STRCONNECT);
// Create a new command Object oledbcommand objcommand = new OLEDBCOMMAND ();
// set the property = Objconnect; objcommand.commandtype = commandtype.text; objcommand.comMandtext = strselectbooks;
// Create a new dataAdapter Object oledbdataadapter objdataadapter = new oledbdataadapter (); // and assign the command Object to it objDataadapter.selectCommand = Objcommand;
// Get the data from the "booklist" Table in the database and // put it into a table named "books" in the dataset object objdataadapter.fill (ObjDataSet, "Books");
// change the SELECT statement in the Command object objCommand.CommandText = strSelectAuthors; // then get data from "BookAuthors" table into the DataSet objDataAdapter.Fill (objDataSet, "Authors");} catch (Exception objError) {// display Error Details OutError.INNerhtml = " * Error While Accationsing Data b>.
" Objerror.Message "
" Objerror.Source; Return; // and stop execution}
// now that the DataSet is filled, we can modify the tables it contains // declare variables to refer to the DataTable and a DataColumn objects DataTable objParentTable = objDataSet.Tables [ "Books"]; // parent table DataTable objChildTable = objDataSet. Tables ["authors"]; // Subsheet Datacolumn ObjParentColumn = ObjParentTable.Columns ["ISBN"]; // Parent table's ISBN column Datacolumn ObjchildColumn = ObjChildTable.Columns ["ISBN"]; // Table ISBN column
// create a new UniqueConstraint object and add to Constraints collection // create a primary key constraint UniqueConstraint objUnique = new UniqueConstraint ( "Unique_ISBN", objParentColumn); // Unique_ISBN is the constraint name, objParentColumn is the primary key constraint column objParentTable.Constraints.Add (objUnique) ; // Add constraint // prevent the column from accepting null value objparentcolumn.allowdbnull = false;
// create an array of columns containing this column online = objcolumnarray [0] = objparentcolumn; = objparentcolumn;
// and set this array as the columns for the primary key of the table // Specify the primary key (is a Datacolumn array, ensuring only the only label. There is only the ISBN list) ObjparentTable.PrimaryKey = ObjColumnarray;
// now we can process the child table named "Authors" // create an array of columns containing the ISBN and Lastname columns objColumnArray = new DataColumn [2]; objColumnArray [0] = objChildColumn; // the ISBN column objColumnArray [1] = objChildTable.Columns [ "Lastname"]; // prevent either of these columns containing Null objColumnArray [0] .AllowDBNull = false; objColumnArray [1] .AllowDBNull = false; // set this column array as the primary key // specified Primary key (array consisting of two columns of ISBN and LastName) ObjchildTable.PrimaryKey = ObjColumnarray;
// create a new // specified foreign key ForeignKeyConstraint objFKey = new ForeignKeyConstraint ForeignKeyConstraint object ( "FK_BookAuthors", objParentColumn, objChildColumn); // set the "update" properties objFKey.DeleteRule = Rule.Cascade; // cascade objFKey.UpdateRule = Rule.cascade;
// and add it to the constraints collection objchildtable.constraints.add (objfkey);
/ / -------------------------------------------------------------------------------------------- -------------- // Now We're Ready to Display The Contents of the DataSet Object // Bind The Collection of Tables To The First DataGrid ON The page DGRTables.DataSource = ObjDataSet.Tables DGRTABLES.DATABIND ();
// bind the collection of constraints to the second data.datasource = objdataset.tables ["books"]. ConsTRAINTS; DGRBOOKCONS.DATABIND ();
// Bind The Collection of Constraints To The Third DataGrid on the page DGRAUTHORCONS.DATASOURCE = ObjDataSet.tables ["authors"]. ConsTRAINTS; DGRAUTHORCONS.DATABIND ();
// create a DataView object to use with the tables in the DataSet DataView objDataView = new DataView (); // get the default view of the Books table into the DataView object objDataView = objDataSet.Tables [ "Books"] DefaultView;. / / and bind it to the next datadata.datasource = objDataView; DGRbooksData.DATABIND ();
// The Do The Same for the Authors Table ObjDataView = Objdataset.tables ["Authors"]. DefaultView; DgrauthorsData.DataSource = ObjDataView; DGRAUTHORSDATA.DATABIND ();
} script>