同上篇,主要是参考dOOdad文档,并加入了一些自己的理解。
1. 数据绑定:
(1) 绑定到DataGrid(WebApp)
if
(emps.LoadAll())
...
{
dvEmps = emps.DefaultView;
dgEmps.DataSource = dvEmps;
dgEmps.DataBind();
}
(2) 绑定到ComboBox(WinApp)
//
get data
emps.Query.AddResultColumn(Employees.ColumnNames.EmployeeID);
emps.Query.AddResultColumn(Employees.ColumnNames.LastName);
emps.Query.AddOrderBy(Employees.ColumnNames.LastName,
WhereParameter.Dir.ASC);
emps.Query.Load();
//
bind to combobox
cmbEmployees.DisplayMember
=
Employees.ColumnNames.LastName;
cmbEmployees.ValueMember
=
Employees.ColumnNames.EmployeeID;
cmbEmployees.Datasource
=
emps.DefaultView;
(3) 绑定到DropDownList(WebApp)
//
get data
emps.Query.AddResultColumn(Employees.ColumnNames.EmployeeID);
emps.Query.AddResultColumn(Employees.ColumnNames.LastName);
emps.Query.AddOrderBy(Employees.ColumnNames.LastName,
WhereParameter.Dir.ASC);
emps.Query.Load();
//
bind to drop-down list
ddlEmployees.Datasource
=
emps.DefaultView;
ddlEmployees.DataTextField
=
Employees.ColumnNames.LastName;
ddlEmployees.DataValueField
=
Employees.ColumnNames.EmployeeID;
ddlEmployees.Databind();
2. 特殊函数:执行自定义存储过程和SQL语句
Examples:
(1)LoadFromSql: (从数据库中检索并返回检索结果集,查询结果保存到DataTable中)
ListDictionary Parms
=
new
ListDictionary();
Parms.Add(
"
@parameterName1
"
, value1);
Parms.Add(
"
@parameterName2
"
, value2);
parms.Add……
if
(
this
.LoadFromSql(
"
proc_GetSpecialEmployee
"
, Parms))
...
{
...
}
(2)LoadFromSqlNoExec: (执行无结果集的存储过程,返回受影响的行数)
int NbrOfChecks = this.LoadFromSqlNoExec("proc_CalculatePayroll");
例如:删除一条记录,dOOdad提供的文档中的方法是:先LoadByPrimaryKey,再MarkAsDeleted,最后再Save进数据库;这个过程要对数据库进行两次操作。实际上,我们可以只操作一次数据库(直接用dOOdads模板生成的存储过程),而不用先Load。
ListDictionary Parms = new ListDictionary();
Parms.Add("@ID", empID);
int effectLine = this.LoadFromSqlNoExec("proc_EmployeeDelete",Parms);
(3)LoadFromSqlReader:(返回SqlDataReader)
SqlDataReader rdr = this.LoadFromSqlReader("proc_GetSpecialEmployee", Parms) as SqlDataReader;
(4)LoadFromSqlScalar:(返回单个值)
DateTime EarliestHireDate;
ListDictionary Parms = new ListDictionary();
Parms.Add("@Active", 1);
EarliestHireDate = Convert.ToDateTime(this.LoadFromSqlScalar("GetEarliestHireDate",Parms));
(5)LoadFromRawSql:
this.LoadFromRawSql("SELECT MIN(EM_HireDate) FROM tEmployees WHERE EM_Active = {0}","1");
3. 事务处理:
TransactionMgr tx
=
TransactionMgr.ThreadTransactionMgr();
try
...
{
Employees emps = new Employees();
emps.AddNew();
emps.FirstName = "Jimmy";
emps.LastName = "Doe";
Products prds = new Products();
prds.AddNew();
prds.ProductName = " Lunch Box ";
prds.Discontinued = false;
tx.BeginTransaction();
emps.Save();
prds.Save();
tx.CommitTransaction();
}
catch
(Exception ex)
...
{
tx.RollbackTransaction();
TransactionMgr.ThreadTransactionMgrReset();
}