The following notes were taken during a lecture in COMP 241 .NET Web Applications at Camosun College by Rob Thorndyke. These notes are here mainly to help me study, but if you can find some guidance through them then all the better!
Web Page
|
GridView (DataBound Control, DataSource Property)
|– DataTable (pure data – no UI)
|– Adapter Object — Database
DataTable & DataColumn
Example Code:
DataTable dataTable = new DataTable(); DataColumn idColumn = new DataColumn( "id" ); idColumn.DataType = typeof( string ); // DataColumn has all the properties you would expect to find if you were creating a database field idColumn.MaxLength = 10; // Default: -1 idColumn.Unique = true; // Default: false idColumn.AllowDBNull = false; // Default: true; // Add the column to the dataTable dataTable.Columns.Add( idColumn ); // OR dataTable.Columns.Add( "id", typeof( string ) ); DataColumn column = dataTable.Columns[ "id" ]; // --------------------------- DataColumn salaray = new DataColumn( "salary" ); salary.DataType = typeof( Decimal ); salary.DefaultValue = 0.00m; // m = decimal literal dataTable.Columns.Add( salary ); // Decimal - Fixed precision float - good for money // Hex: int t = 0xc4;
DataRow
Example Code:
DataRow row1 = dataTable.newRow(); row1["id"] = "x123"; row1["salary"] = 11043.00m; dataTable.Rows.Add(row1); // Alternatively dataTable.Rows.Add("Z417", 32147.00m); // Alternatively adding a row with an object array dataTable.Rows.Add(new object[] { "P112", 7417.00m } );
DataRow State
DataRow State
- Detached – not in a Table
- Added – just added; never committed
- Unchanged – no changes since last commit
- Modified – some changes since last commit
- Deleted – just deleted
DataRow Version
- Current
- Default
- Original
- Proposed
Code:
row1.AcceptChanges(); // --> "commit" row1.RejectChanges(); // --> "roll back" label1.Text = "" + row1.RowState;
DataSet
A collection of DataTables and DataRelations
DataSet ds = new DataSet("db"); DataTable table1 = ds.Tables.Add("table1"); table1.Columns.Add("id", typeof(Guid)); // Guid: Global Unique Identifier table1.Columns.Add("name", typeof(string)); table1.PrimaryKey = new DataColumn[] { table1.Columns["id"] }; DataTable table2 = ds.Tables.Add("table2"); table2.Columns.Add("id", typeof(Guid)); table2.Columns.Add("style", typeof(string)); table2.Columns.Add("foreign_key", typeof(Guid)); table2.PrimaryKey = new DataColumn[] { table2.Columns["id"] }; ds.Relations.Add("table1_table2_relation", table1.Columns["id"], table2.Columns["foreign_key"]);
Web Page
–|–GridView
——-|–DataSet
————|–DataAdapter
—————–|–database