Doodads Quick Ref CSharp
Doodads Quick Ref CSharp
TABLE OF CONTENTS
TransactionMgr 13
Object model 14
Addenda Creating a typed dataset 15
PREPARATION
Install MyGeneration and add the dOOdads project to your solution TOC
1. Download from http://www.mygenerationsoftware.com and install.
2. The installer puts the application in Program Files.
3. Add the dOOdads project to your Visual Studio solution:
a. Right-click on the Solution item and choose "Add Existing Project".
b. Navigate to the dOOdads project (C:\Program Files\MyGeneration\
Architectures\dOOdads\CSharp\MyGeneration.dOOdads\
dOOdads.csproj) and select. The dOOdads project will now appear in your
solution.
c. In the DbAdapters folder, open the "Entity" corresponding to your database
(e.g. “SqlClientEntity”). Find "Build Action" in "Properties" and set to "Compile".
Repeat for the "DynamicQuery" adapter.
4. Build the dOOdads project.
With MyGeneration:
}
}
Enter the connection string in the AppSettings section of the config file TOC
1. Put the connection string in the web.config (for web apps) or app.config (for
WinForms apps) file and name it "dbConnection".
2. If the connection string is not explicitly assigned in the dOOdad, the dOOdad will
look in the config file for "dbConnection".
For processing related to the business entity that normal dOOdads methods won't
handle, consider using the special LoadFromSql* methods (see SPECIAL FUNCTIONS) here
in the concrete class.
COMMON TASKS
Prep
Employees emps = new Employees();
int empID;
Calculated columns (used by your application in the dataset, but not stored in the database) TOC
AddColumn
SetColumn
GetColumn
IsColumnNull
SetColumnNull
Example:
if(emps.LoadAll())
{
DataColumn col = emps.AddColumn("FullName",
Type.GetType("System.String"));
col.Expression = Employees.ColumnNames.LastName +
"+ ', ' + " + Employees.ColumnNames.FirstName;
string fullName = emps.GetColumn("FullName") as string;
}
ConnectionString TOC
Define the connection string in your config file, naming it "dbConnection".
If you choose not to do that or you are using more than one database, you can use the
ConnectionString property of the dOOdad.
emps.ConnectionString =
"User=me;Password=pw;Database=Employees;DataSource=MyServer"
For example:
emps.Salary and emps.s_Salary
emps.HireDate and emps.s_HireDate
DYNAMIC QUERY
The dynamic query property of the dOOdad (MyDoodad.Query) allows you to select
records in an ad hoc fashion without having to write a bunch of little stored procedures.
(And its design precludes the possibility of SQL-injection attacks.)
Conjunctions (WhereParameter.Conj)
• And
• Or
• UseDefault
Directions (WhereParameter.Dir)
• ASC
• DESC
Operands (WhereParameter.Operand)
• Between
• Equal
• GreaterThan
• GreaterThanOrEqual
• In
• IsNotNull
• IsNull
• LessThan
• LessThanOrEqual
• Like
• NotEqual
• NotIn
• NotLike
Limit columns returned (Save() cannot be called after limiting columns.) TOC
emps.Query.AddResultColumn(Employees.ColumnNames.EmployeeID);
emps.Query.AddResultColumn(Employees.ColumnNames.LastName);
emps.Query.Load();
Order By TOC
emps.Query.AddOrderBy(Employees.ColumnNames.HireDate,
WhereParameter.Dir.DESC);
Parentheses TOC
emps.Query.OpenParenthesis();
emps.Query.CloseParenthesis();
GenerateSQL TOC
A diagnostic function that returns the SQL statement created for the dynamic query.
After calling this you cannot load the object. Better to use LastQuery.
LastQuery TOC
A string property that contains the SQL text of the most recently generated SQL
statement.
ReturnReader TOC
SqlDataReader reader = emps.Query.ReturnReader() as SqlDataReader;
DATA BINDING
//bind to combobox
cmbEmployees.DisplayMember = Employees.ColumnNames.LastName;
cmbEmployees.ValueMember = Employees.ColumnNames.EmployeeID;
cmbEmployees.Datasource = emps.DefaultView;
SPECIAL FUNCTIONS
These functions can be used to extend the concrete class (in fact, they cannot be accessed
outside of the concrete class). See the examples on the next page.
Examples:
LoadFromSql
ListDictionary Parms = new ListDictionary();
Parms.Add(Employees.Parameters.EmployeeID, 152);
if(this.LoadFromSql("proc_GetSpecialEmployee", Parms))
{
...
}
LoadFromSqlNoExec
int NbrOfChecks = this.LoadFromSqlNoExec("proc_CalculatePayroll");
LoadFromSqlReader
SqlDataReader rdr =
this.LoadFromSqlReader("proc_GetSpecialEmployee", Parms)
as SqlDataReader;
LoadFromSqlScalar
DateTime EarliestHireDate;
Parms.Clear();
Parms.Add("@Active", 1);
EarliestHireDate =
Convert.ToDateTime(this.LoadFromSqlScalar("GetEarliestHireDate",
Parms));
LoadFromRawSql
this.LoadFromRawSql("SELECT MIN(EM_HireDate) FROM tEmployees WHERE EM_Active =
{0}","1");
//Now reload that single record into a new Employees object and save it
Employees empsClone = new Employees();
empsClone.FromXml(xml);
empsClone.Save();
TOC
TRANSACTIONMGR
TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
try
{
Employees emps = new Employees();
emps.AddNew();
emps.FirstName = "Jimmy";
emps.LastName = "Doe";
tx.BeginTransaction();
emps.Save();
prds.Save();
tx.CommitTransaction();
}
catch(Exception ex)
{
tx.RollbackTransaction();
TransactionMgr.ThreadTransactionMgrReset();
}
TOC
dOOdads Object Model
(This is the C# flavor. There are slight differences in the Enums in the VB version.)
ADDENDA