MyGeneration学习笔记(8) :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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值