dataset 數(shù)據(jù)集應(yīng)用實(shí)例(詳細(xì))_第1頁(yè)
dataset 數(shù)據(jù)集應(yīng)用實(shí)例(詳細(xì))_第2頁(yè)
dataset 數(shù)據(jù)集應(yīng)用實(shí)例(詳細(xì))_第3頁(yè)
dataset 數(shù)據(jù)集應(yīng)用實(shí)例(詳細(xì))_第4頁(yè)
dataset 數(shù)據(jù)集應(yīng)用實(shí)例(詳細(xì))_第5頁(yè)
已閱讀5頁(yè),還剩17頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

dataset數(shù)據(jù)集應(yīng)用實(shí)例(詳細(xì))一、數(shù)據(jù)集基本應(yīng)用1.表格新增記錄方式一:利用BindingSource的AddNew〃新增記錄,推薦使用,光標(biāo)位置處于當(dāng)前新增記錄,且正處理編輯狀態(tài)DataRowthisRow=((DataRowView)usersBindingSource.AddNew()).Row;thisRow[”O(jiān)ID"]=5;thisRow["CNAME"]=噺增用戶";thisRow[”sex"]="m";方式二:利用DataTable的NewRow〃新增記錄(不建議使用,因?yàn)檫@種方式Rows.Add時(shí)并不處于編輯狀態(tài)時(shí)會(huì)受約束影響,且新增時(shí)光標(biāo)不會(huì)自動(dòng)移動(dòng)該條記錄)DataRowthisRow=userDataSet.Tables["Users"].NewRow();thisRow[”O(jiān)ID"]=5;thisRow["CNAME"]=噺增用戶";thisRow["sex"]="m";userDataSet.Tables["Users"].Rows.Add(thisRow);2?表格刪除記錄方式一:利用BindingSource的RemoveCurrentif(usersBindingSource.Current!=nuII)〃刪除當(dāng)前記錄,推薦使用

usersBindingSource.RemoveCurrent();方式二:利用DataRowCollection的Remove更新〃刪除當(dāng)前記錄,不推薦使用,這種方式不會(huì)記錄到RowState中,保存時(shí)不會(huì)更新DataRowthisRow=getCurrentDataRow(usersBindingSource);if(thisRow!=null)userDataSet.Tables["Users"].Rows.Remove(thisRow);方式三:利用DataRow的Delete〃刪除當(dāng)前記錄,不推薦使用,BindingSource可以更簡(jiǎn)潔DataRowthisRow=getCurrentDataRow(usersBindingSource);if(thisRow!=null)thisRow.Delete();3?表格修改記錄方式一:利用DataRowObject[列名]直接修改DataRowthisRow=getCurrentDataRow(usersBindingSource);if(thisRow!=null){thisRow.BeginEdit();thisRow["CNAME"]="修改的名稱(chēng)";thisRow.EndEdit();4.表格查找和篩選記錄方式一:利用DataRowCollection.find查找DataColumn[]keys=newDataColumn[1];keys[0]=userDataSet.Tables["Users"].Columns["OID"];userDataSet.Tables["Users"].PrimaryKey=keys;DataRowfindRow=userDataSet.Tables["Users"].Rows.Find("1");if(findRow==null){MessageBox.Show('沒(méi)有找到");}else{MessageBox.Show("成功找到,CNAME="+findRow["CNAME"]);}方式二:利用BindingSource.find查找inti=usersBindingSource.Find("OID","1");if(i>=0)MessageBox.Show("成功找到,CNAME="+userDataSet.Tables["users"].Rows[i]["CNAME"]);方式三:利用DataTable.Select獲得DataRow數(shù)組DataRow[]AryDr=userDataSet.Tables["users"].Select("OID>1");for(inti=0;i<AryDr.Length;i++){DataRowdr=AryDr[i];MessageBox.Show(Convert.ToString((int)dr["OID"]));}5?表格記錄的移動(dòng)方式一:采用BindingSource的方法或position屬性實(shí)現(xiàn)?!ㄖ付ǘㄎ坏侥囊恍小甈osition不會(huì)隨表格列排序而變化,0不一定就是表格的第一行usersBindingSource.Position=0;〃移動(dòng)到上一條,對(duì)于表格列排序后,上一條不定是界面顯示表格的上一條usersBindingSource.MovePrevious();usersBindingSource.MoveNext();〃移動(dòng)到下一條usersBindingSource.MoveFirst();usersBindingSource.MoveLast();表格的過(guò)濾方式一:利用BindingSource的Filter來(lái)實(shí)現(xiàn)usersBindingSource.Filter="OID>1";數(shù)據(jù)集清空方式一:利用DataTable.Clear(),注意這種不會(huì)保留刪除狀態(tài),保存時(shí)不會(huì)真正刪除userDataSet.Tables["users"].CIear();方式二:利用DataTable.Rows.Clear刪除,注意這種不會(huì)保留刪除狀態(tài),保存時(shí)不會(huì)真正刪除userDataSet.Tables["users"].Rows.Clear();方式三:利用BindingSource.RemoveCurrent循環(huán)刪除全部記錄,這種就會(huì)保留刪除狀態(tài)。while(usersBindingSource.Current!=null)usersBindingSource.RemoveCurrent();8數(shù)據(jù)集數(shù)據(jù)和結(jié)構(gòu)的復(fù)制方式一:整個(gè)數(shù)據(jù)集的復(fù)制DataSetcopyDS=userDataSet.Copy();方式二:只復(fù)制單個(gè)表DataSetcopyDS=newDataSet();copyDS.Tables.Add(userDataSet.Tables["users"].Copy());方式三:只復(fù)制數(shù)據(jù)集的結(jié)構(gòu)copyDS=userDataSet.Clone();MessageBox.Show(copyDS.Tables["users"].Rows.Count.ToString());獲取臟數(shù)據(jù)方式一:整個(gè)數(shù)據(jù)集的臟數(shù)據(jù)copyDS=userDataSet.GetChanges();方式二:獲取單個(gè)表的臟數(shù)據(jù)DataTabledt=userDataSet.Tables["users"].GetChanges();數(shù)據(jù)集的數(shù)據(jù)合并方式一:整個(gè)數(shù)據(jù)集的DataSet.Merge合并ds.Merge(userDataSet);方式二:?jiǎn)蝹€(gè)表的DataTable.Merge合并ds.Merge(userDataSet.Tables["users"]);數(shù)據(jù)集的數(shù)據(jù)回滾方式一:數(shù)據(jù)集的數(shù)據(jù)回滾userDataSet.RejectChanges();方式二:數(shù)據(jù)表的數(shù)據(jù)回滾userDataSet.Tables["users"].RejectChanges();方式三:數(shù)據(jù)行的數(shù)據(jù)回滾DataRowdr=getCurrentDataRow(usersBindingSource);if(dr!=null)dr.RejectChanges();數(shù)據(jù)集從數(shù)據(jù)庫(kù)取數(shù)方式一:利用SqlDataAdapter.Fill來(lái)填充數(shù)據(jù)表this.usersTableAdapter.Fill(this.userDataSet.Users);數(shù)據(jù)集更新到數(shù)據(jù)庫(kù)方式一:利用SqlDataAdapter.Update來(lái)更新到數(shù)據(jù)庫(kù)this.Validate();this.usersBindingSource.EndEdit();if(this.userDataSet.HasChanges()){this.usersTableAdapter.Update(this.userDataSet.Users);MessageBox.Show('保存成功!");}判斷數(shù)據(jù)集變更方式一:利用DataSet.HasChanges()15獲取數(shù)據(jù)集表列集合方式一:利用DataTable.ColumnsDataColumnCollectiondcc=userDataSet.Tables["users"].Columns;for(inti=0;i<dcc.Count;i++){DataColumndc=dcc[i];MessageBox.Show(dc.ColumnName);}16獲取屬于該表的行的集合方式一:利用DataTable.RowsDataRowCollectiondrc=userDataSet.Tables["users"].Rows;for(inti=0;i<drc.Count;i++)DataRowdr=drc[i];MessageBox.Show((string)dr["CNAME"]);}17獲取或設(shè)置存儲(chǔ)在指定列中的數(shù)據(jù)方式一:利用DataRowObject[列名]來(lái)訪問(wèn)或設(shè)置。DataRowdr=getCurrentDataRow(usersBindingSource);MessageBox.Show((string)dr["CNAME"]);18獲取記錄行的狀態(tài)。方式一:利用DataRow.RowState獲取DataRowdr=getCurrentDataRow(usersBindingSource);switch(dr.RowState){caseDataRowState.Added:MessageBox.Show(噺增的記錄'");break;caseDataRowState.Deleted:MessageBox.Show(刪除的記錄'");break;caseDataRowState.Detached:MessageBox.Show("不屬于任何DataRowCollection的狀態(tài)”);break;caseDataRowState.Modified:MessageBox.Show("修改的記錄'");break;caseDataRowState.Unchanged:MessageBox.Show("未變化的記錄"");break;default:break;};19對(duì)記錄行開(kāi)始編輯操作、取消對(duì)該行的當(dāng)前編輯、終止發(fā)生在該行的編輯方式一:利用DataRow的BeginEdit、CancelEdit、EndEditDataRowdr=getCurrentDataRow(usersBindingSource);dr.BeginEdit();dr["CNAME"]="yy";if(((string)dr["CNAME",DataRowVersion.Original]).Equals("YY"))dr.CancelEdit();elsedr.EndEdit();20獲取或設(shè)置列中是否允許空值方式一:利用DataColumn的AllowDBNull,注意這樣可以允許空字符串userDataSet.Tables["users"].Columns["tel"].AllowDBNull=false;21指示列自動(dòng)遞增方式一:利用DataColumn的AutoIncrement指示DataColumncolumn=newDataColumn();column.DataType=System.Type.GetType("System.lnt32");column.AutoIncrement=true;column.AutoIncrementSeed=1000;column.AutolncrementStep=10;//AddthecolumntoanewDataTable.DataTabletable=newDataTable("table");table.Columns.Add(column);DataRowdr=table.NewRow();MessageBox.Show(Convert.ToString((int)dr[0]));DataRowdr1=table.NewRow();MessageBox.Show(Convert.ToString((int)d門(mén)[0]));22獲取列名方式一:利用DataColumn.ColumnName23在創(chuàng)建新行時(shí)獲取或設(shè)置列的默認(rèn)值方式一:利用DataColumn.DefaultValue24獲取設(shè)置列的只讀方式一:DataColumn.ReadonlyuserDataSet.Tables["users"].Columns["tel"].ReadOnly=true;〃這一句會(huì)出錯(cuò),程序?qū)懭攵疾辉试SuserDataSet.Tables["users"].Rows[O]["tel"]="111";25獲取數(shù)據(jù)集中的數(shù)據(jù)類(lèi)型方式一:利用DataColumn.DataTypeDataColumnCollectiondcc=userDataSet.Tables["users"].Columns;for(inti=0;i<dcc.Count;i++){DataColumndc=dcc[i];MessageBox.Show(dc.DataType.ToString());}26計(jì)算列表達(dá)式的設(shè)置方式一:利用DataColumn.ExpressionuserDataSet.Tables["users"].Columns["totalMoney"].Expression="OIDTOO"27指示列的每一行中的值是否必須是唯一方式一:利用DataColumn.Unique28獲取包含在DataSet中的表的集合方式一:利用DataSet的TablesDataTableCollectionAryTable=userDataSet.Tables;MessageBox.Show(AryTable[0].TableName+""+AryTable[1].TableName);29獲取DataSet所包含的數(shù)據(jù)的自定義視圖方式一:利用DataSet的DefaultViewManager方式二:利用DataTable的DefaultView表格記錄新增、刪除、修改的控制方式一:利用BindingSource的AllowEdit、AllowNew、AllowRemoveusersBindingSource.AllowNew=false;獲取表格的記錄數(shù)方式一:利用BindingSource.CountMessageBox.Show(usersBindingSource.Count.ToString());方式二:利用DataTable.Rows.Count32.獲取表格的當(dāng)前行方式一:利用bindingSource.CurrentpublicstaticDataRowgetCurrentDataRow(BindingSourcebindSource)if(!typeof(DataRowView).lsInstanceOfType(bindSource.Current))returnnull;DataRowViewdrv=(DataRowView)bindSource.Current;if(drv==null)returnnull;elsereturndrv.Row;}33.獲取表格當(dāng)前項(xiàng)的索引方式一:利用BindingSource.PositionMessageBox.Show(usersBindingSource.Position.ToString());二、數(shù)據(jù)集其它應(yīng)用1.其它1.1指示DataTable中的字符串比較是否區(qū)分大小寫(xiě)方式一:指示DataSet的CaseSensitiveuserDataSet.CaseSensitive=true;方式二:扌旨示DataTable的CaseSensitiveuserDataSet.Tables["users"].CaseSensitive=true;DataRow[]AryDR=userDataSet.Tables["users"].Select("CNAME='yy'");1.2指示在嘗試執(zhí)行任何更新操作時(shí)是否遵循約束規(guī)則方式一:指示DataSet的EnforceConstraintsuserDataSet.EnforceConstraints=false;DataRowthisRow=userDataSet.Tables["Users"].NewRow();thisRow[”O(jiān)ID"]=5;thisRow[”sex"]="m";userDataSet.Tables["Users"].Rows.Add(thisRow);〃如果沒(méi)有置EnforceConstraints,則會(huì)添加失敗。1.3獲取與DataSet相關(guān)的自定義用戶信息的集合方式一:利用DataSet的ExtendedPropertiesPropertyCollectionproperties=userDataSet.ExtendedProperties;//AddatimestampvaluetothePropertyCollection.if(!properties.ContainsKey("TimeStamp"))properties.Add("TimeStamp",DateTime.Now);PropertyCollectionproperties1=userDataSet.ExtendedProperties;MessageBox.Show(((DateTime)properties1["TimeStamp"]).ToString());方式二:利用DataTable的ExtendedPropertiesPropertyCollectionproperties=userDataSet.Tables["users"].ExtendedProperties;//AddatimestampvaluetothePropertyCollection.if(!properties.ContainsKey("TimeStamp"))properties.Add("TimeStamp",DateTime.Now);PropertyCollectionproperties1=userDataSet.Tables["users"].ExtendedProperties;MessageBox.Show(((DateTime)properties1["TimeStamp"]).ToString());1.4獲取用于將表鏈接起來(lái)并允許從父表瀏覽到子表的關(guān)系的集合方式一:利用DataSet的RelationsDataColumn[]aryChildColumn=userDataSet.Relations[O].ChildColumns;DataColumn[]aryParentColumn=userDataSet.Relations[0].ParentColumns;MessageBox.Show(aryChildColumn[0].ColumnName+""+aryParentColumn[0].ColumnName);1.5為每個(gè)DataTable返回帶有一個(gè)結(jié)果集的DataTableReader方式一:利用DataSet.CreateDataReader()using(DataTableReaderreader=userDataSet.CreateDataReader()){do{if(!reader.HasRows){Console.WriteLine("EmptyDataTableReader");else{while(reader.Read())//下一個(gè)數(shù)據(jù)行{for(inti=0;i<reader.FieldCount;i++){Console.Write(reader[i]+"");}Console.WriteLine();}}Console.WriteLine("========================");}while(reader.NextResult());〃下一個(gè)數(shù)據(jù)表}方式二:利用DataTable.CreateDataReader()1.6返回存儲(chǔ)在DataSet中的數(shù)據(jù)的XML表示形式方式一:利用DataSet.GetXml();MessageBox.Show(userDataSet.GetXml());1.7獲取或設(shè)置充當(dāng)數(shù)據(jù)表主鍵的列的數(shù)組方式一:設(shè)置DataTable.PrimaryKeyDataColumn[]keys=newDataColumn[1];keys[O]=userDataSet.Tables["Users"].Columns["OID"];userDataSet.Tables["Users"].PrimaryKey=keys;1.8在加載數(shù)據(jù)時(shí)關(guān)閉通知、索引維護(hù)和約束方式一:DataTable.BeginLoadData()和EndLoadData()。1.9將DataRow復(fù)制到DataTable中,保留任何屬性設(shè)置以及初始值和當(dāng)前值方式一:利用DataTable.ImportRowDataSetcopyDS=newUserDataSet();copyDS.Tables["users"].ImportRow(userDataSet.Tables["users"].Rows[0]);MessageBox.Show(copyDS.Tables["users"].Rows[O]["cname",DataRowVersion.Original]+""+copyDS.Tables["users"].Rows[0]["cname",DataRowVersion.Current]);1.10查找和更新特定行。如果找不到任何匹配行,則使用給定值創(chuàng)建新行方式一:利用DataTable.LoadDataRowDataRownewRow=userDataSet.Tables["users"].NewRow();newRow["OID"]=1;newRow["CNAME"]="renameyy";newRow["SEX"]="f";userDataSet.Tables["users"].BeginLoadData();//LoadDataRow,以主鍵查找記錄,當(dāng)記錄存在時(shí)只更新,不存在時(shí)新增記錄DataRowrow=userDataSet.Tables["user

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論