public void Iteration() { Employees emps = new Employees(); if (emps.LoadAll()) { string lastName; // Iteration walks the DataTable.DefaultView, see the FilterAndSort // sample for further clarification. do { lastName = emps.LastName; }while(emps.MoveNext()); emps.Rewind(); do { lastName = emps.LastName; }while(emps.MoveNext()); } //----------------------------------------------------------- // Moral: //----------------------------------------------------------- // Iteration is simple, you can rewind and restart at any time //----------------------------------------------------------- }
public void AddColumn() { Employees emps = new Employees(); if (emps.LoadAll()) { DataColumn col = emps.AddColumn("FullName", Type.GetType("System.String")); col.Expression = Employees.ColumnNames.LastName + "+ ', ' + " + Employees.ColumnNames.FirstName; string fullName; do { fullName = emps.GetColumn("FullName") as string; }while(emps.MoveNext()); } //----------------------------------------------------------- // Moral: //----------------------------------------------------------- // Of course if( you add a column Dynamically as the code does above // you'll have no strongly typed accessor like emps.FullName, but you // can use GetColumn() to access dynamic columns. // // Never use this to access other fields in your business entity // although it may work, it's poor programming and always use // your ColumnNames property and not hardcoded strings when possible //----------------------------------------------------------- }
public void FilterAndSort() { Employees emps = new Employees(); if (emps.LoadAll()) { // After you load your business entity you can apply Sort and Filter, // you could also potentially do this in the Where clause too emps.Filter = Employees.ColumnNames.City + " = 'Berlin'"; emps.Sort = Employees.ColumnNames.LastName + " DESC"; // Filter might have "hidden" all of the rows if (emps.RowCount > 0) { string lastName; // Remember, iteration walks the DataTable.DefaultView which is // effected by Sort and Filter, so you'll only get Employees who // live in Berlin, and they'll fed to your MoveNext() loop in decending // order by LastName do { lastName = emps.LastName; }while(emps.MoveNext()); } //----------------------------------------------------------- // Moral: //----------------------------------------------------------- // Sort and Filter can be great ways to implement data grid // filtering in your user interface when you don't want to requery // the database, just bind your grid to emps.DefaultView //----------------------------------------------------------- } }
public void DemonstrateBulkUpdates() { Employees emps = new Employees(); if (emps.LoadAll()) { // Modify the LastName column in every row do { emps.LastName += "W"; }while(emps.MoveNext()); } // Rewind and mark the first row as Deleted //emps.Rewind(); //emps.MarkAsDeleted(); // Add a new row and fill it in emps.AddNew(); emps.FirstName = "Jimmy"; emps.LastName = "Lunch Box"; // Save all modifications, deletes, and new rows emps.Save(); //----------------------------------------------------------- // Moral: //----------------------------------------------------------- // This is a very nice way to work. When you generate your // stored procedures using 'SQL_StoredProcs.vbgen' you'll find // it is pretty smart. It makes any identity columns // and or computed columns as OUTPUT parameters. Thus, after Save() // any new rows or updated rows have their identity // columns or calulated columns already in them, no // requerying the database // // You never have to use a transaction when saving a single object. // The dOOdad architecture always does this for you. //----------------------------------------------------------- }
public void AddColumn() { Employees emps = new Employees(); if(emps.LoadAll()) { DataColumn col = emps.AddColumn("FullName", Type.GetType("System.String")); col.Expression = Employees.ColumnNames.LastName + "+ ', ' + " + Employees.ColumnNames.FirstName; string fullName; do fullName = emps.GetColumn("FullName") as string; while(emps.MoveNext()); } //----------------------------------------------------------- // Moral: //----------------------------------------------------------- // Of course if( you add a column Dynamically as the code does above // you'll have no strongly typed accessor like emps.FullName, but you // can use GetColumn() to access dynamic columns. // // Never use this to access other fields in your business entity // although it may work, it's poor programming and always use // your ColumnNames property and not hardcoded strings when possible //----------------------------------------------------------- }
public void DemonstrateBulkUpdates() { Employees emps = new Employees(); if(emps.LoadAll()) { // Modify the LastName column in every row do emps.LastName += "W"; while(emps.MoveNext()); } // Rewind and mark the first row as Deleted //emps.Rewind(); //emps.MarkAsDeleted(); // Add a new row and fill it in emps.AddNew(); emps.FirstName = "Jimmy"; emps.LastName = "Lunch Box"; // Save all modifications, deletes, and new rows emps.Save(); //----------------------------------------------------------- // Moral: //----------------------------------------------------------- // This is a very nice way to work. When you generate your // stored procedures using 'SQL_StoredProcs.vbgen' you'll find // it is pretty smart. It makes any identity columns // and or computed columns as OUTPUT parameters. Thus, after Save() // any new rows or updated rows have their identity // columns or calulated columns already in them, no // requerying the database // // You never have to use a transaction when saving a single object. // The dOOdad architecture always does this for you. //----------------------------------------------------------- }
public void FilterAndSort() { Employees emps = new Employees(); if(emps.LoadAll()) { // After you load your business entity you can apply Sort and Filter, // you could also potentially do this in the Where clause too emps.Filter = Employees.ColumnNames.City + " = 'Berlin'"; emps.Sort = Employees.ColumnNames.LastName + " DESC"; // Filter might have "hidden" all of the rows if(emps.RowCount > 0) { string lastName; // Remember, iteration walks the DataTable.DefaultView which is // effected by Sort and Filter, so you'll only get Employees who // live in Berlin, and they'll fed to your MoveNext() loop in decending // order by LastName do lastName = emps.LastName; while(emps.MoveNext()); } //----------------------------------------------------------- // Moral: //----------------------------------------------------------- // Sort and Filter can be great ways to implement data grid // filtering in your user interface when you don't want to requery // the database, just bind your grid to emps.DefaultView //----------------------------------------------------------- } }
public void Iteration() { Employees emps = new Employees(); if(emps.LoadAll()) { string lastName; // Iteration walks the DataTable.DefaultView, see the FilterAndSort // sample for further clarification. do lastName = emps.LastName; while(emps.MoveNext()); emps.Rewind(); do lastName = emps.LastName; while(emps.MoveNext()); } //----------------------------------------------------------- // Moral: //----------------------------------------------------------- // Iteration is simple, you can rewind and restart at any time //----------------------------------------------------------- }