MyGeneration:dOOdad提供的資料繫結、特殊函式和事務處理

iDotNetSpace發表於2008-06-24

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();
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-364717/,如需轉載,請註明出處,否則將追究法律責任。

相關文章