.net多語言和數(shù)據(jù)集內(nèi)多數(shù)據(jù)表的處理(2)
2024-07-10 13:03:34
供稿:網(wǎng)友
2 考慮一個數(shù)據(jù)集中有多個數(shù)據(jù)表的問題
好處是什么?
很重要的一點,可以很方便的導航取得相關(guān)的信息,如province.getcityrows ()可以獲得這個省下面的所有城市,而province.countryrow則可以得到這個省所在的國家,是不是比以前通過外鍵再到數(shù)據(jù)庫去查方便的多呢?
伴隨著這種導航而來的是方便的級連更新,比如刪除了父記錄就會自動地所有關(guān)聯(lián)的子記錄刪除,這些都是自動進行的,你不需要多寫一行代碼。
其實,這些都是比較小的功能,更強大的功能在于方便的統(tǒng)計和聚合,考慮這么一個需求:對于商品類別表producttype,我需要計算每一類商品的價格總和并保存到producttype標的total列中,我們以前會怎么做呢?現(xiàn)在我們可以這么寫ds.projecttype.totalcolumn. expression = “sum(child.price)”;這樣是不是很方便呢?再比如,對于學校的班級表class,希望統(tǒng)計每一個班所有學生成績(fraction)的標準差并保存在class表的stdev列中,那么我們可以這么寫ds.class.stdevcolumn.expression = “stdev(child.fraction”就可以了。
數(shù)據(jù)集架構(gòu):采用這種編寫方式開發(fā)的數(shù)據(jù)集架構(gòu)大概是這個樣子
可以看到這個數(shù)據(jù)集里包含了幾乎所有的地址數(shù)據(jù),通過表間的關(guān)聯(lián)我們便可以非常方便的在數(shù)據(jù)集中導航。
如何與數(shù)據(jù)庫同步?
很不幸的,數(shù)據(jù)庫的發(fā)展還沒有趕上步伐,以至于我們在與數(shù)據(jù)庫同步的時候不得不考慮很多,我希望,在sql server的下一個版本中將不再需要我們這么麻煩。
從數(shù)據(jù)庫獲取數(shù)據(jù)時,我們需要實例化多個數(shù)據(jù)適配器,每個數(shù)據(jù)適配器針對一張數(shù)據(jù)表,然后把它們承載的數(shù)據(jù)分別填充到數(shù)據(jù)集中相應的數(shù)據(jù)表中;
而把數(shù)據(jù)更新會數(shù)據(jù)庫的時候,我們也需要實例化多個數(shù)據(jù)適配器,然后依次更新數(shù)據(jù)庫的表,在這個步驟中,你要仔細考慮它們之間的順序,新增記錄的時候,要先主表后子表,刪除的時候要先子表后主表。
如下所示:
public systemdata getsysteminfo(sqlconnection sqlcon)
{
systemdata ds = new systemdata();
sqldataadapter dausers = new sqldataadapter(systemsql.strgetusers,sqlcon);
dausers.fill(ds.users);
sqldataadapter darole = new sqldataadapter(systemsql.strgetrole,sqlcon);
darole.fill(ds.role);
sqldataadapter daruserrole = new sqldataadapter(systemsql.strgetruserrole,sqlcon);
daruserrole.fill(ds.ruserrole);
sqldataadapter damodule = new sqldataadapter(systemsql.strgetmodule,sqlcon);
damodule.fill(ds.module);
sqldataadapter damodulefunction = new sqldataadapter(systemsql.strgetmodulefunction,sqlcon);
damodulefunction.fill(ds.modulefunction);
sqldataadapter darolefunction = new sqldataadapter(systemsql.strgetrolefunction,sqlcon);
darolefunction.fill(ds.rolefunction);
sqldataadapter dauserfunction = new sqldataadapter(systemsql.strgetuserfunction,sqlcon);
dauserfunction.fill(ds.userfunction);
sqldataadapter dausercustomparam = new sqldataadapter(systemsql.strgetusercustomparam,sqlcon);
dausercustomparam.fill(ds.usercustomparam);
return ds;
}
public void insupdusers(systemdata ds)
{
datatableextend[] dts = new datatableextend[4];
dts[0] = new datatableextend(ds.users, "users");
dts[1] = new datatableextend(ds.ruserrole,"ruserrole");
dts[2] = new datatableextend(ds.usercustomparam,"usercustomparam");
dts[3] = new datatableextend(ds.userfunction,"userfunction");
sqlmodify.modifydatabase(dts,dbname);
}
public void upddelusers(systemdata ds)
{
datatableextend[] dts = new datatableextend[4];
dts[0] = new datatableextend(ds.ruserrole,"ruserrole");
dts[1] = new datatableextend(ds.usercustomparam,"usercustomparam");
dts[2] = new datatableextend(ds.userfunction,"userfunction");
dts[3] = new datatableextend(ds.users, "users");
sqlmodify.modifydatabase(dts,dbname);
}
(未完待續(xù))