VB.NET中能不能对DATASET再用SQL语言

前端之家收集整理的这篇文章主要介绍了VB.NET中能不能对DATASET再用SQL语言前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

可以筛选数据,但不能是标准的sql语句: Me.DsUserManager1.Tables(0).Select("id>5andid<20") --------------------------------------------------------------- 1.筛选: dataset.tables("tabname").select("id=1")'相当于sql中WHERE后的条件内容 2.保存到哪?这倒是不知,可能开辟一个内存,也可能是一个临时区.... 应该相当于从一个表中select --------------------------------------------------------------- Select函数返回一个DataRow()对象数组,可用循环方法加入数据集: dimrowasdatarow dimrows()asdatarow=Me.DsUserManager1.Tables(0).Select("id>5andid<20") ifrows.length>0thenDsUserManager1.table(0).clear'重新导入筛选的数据 foreachrowinrows DsUserManager1.table(0).ImportRow(row) next --------------------------------------------------------------- ms-help://MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemDataDataTableClassSelectTopic.htm --------------------------------------------------------------- 类似这样 OdbcCommand1.CommandText="select*fromxx" OdbcDataAdapter1.SelectCommand=OdbcCommand1 OdbcDataAdapter1.Fill(dataset1,"xx") --------------------------------------------------------------- 可以对dataset中的dataview进行筛选。例如:dataview1.rowFilter="Parent_id"=+Parent.id.tostring dataview排序用dataview1.sort="ParentASC" --------------------------------------------------------------- 你要在Table中重新定义一次主键才行,如下: PrivateSubGetRowsByFilter() DimcustomerTableAsDataTable customerTable=newDataTable("Customers") 'Addcolumns customerTable.Columns.Add("id",GetType(Integer)) customerTable.Columns.Add("name",GetType(String)) 'SetPrimaryKey customerTable.Columns("id").Unique=true customerTable.PrimaryKey=newDataColumn(){customerTable.Columns("id")} 'addtenrows DimidAsInteger Forid=1To10 customerTable.Rows.Add(_ newobject(){id,string.Format("customer{0}",id)}) Nextid customerTable.AcceptChanges() 'addanothertenrows Forid=11To20 customerTable.Rows.Add(_ newobject(){id,id)}) Nextid DimstrExprAsString DimstrSortAsString strExpr="id>5" 'SortdescendingbyCompanyNamecolumn. strSort="nameDESC" 'UsetheSelectmethodtofindallrowsmatchingthefilter. DimfoundRowsAsDataRow()=_ customerTable.Select(strExpr,strSort,DataViewRowState.Added) PrintRows(foundRows,"filteredrows") foundRows=customerTable.Select() PrintRows(foundRows,"allrows") EndSub PrivateSubPrintRows(rows()AsDataRow,labelAsString) Console.WriteLine("/n{0}",label) Ifrows.Length<=0Then Console.WriteLine("norowsfound") ExitSub EndIf DimrAsDataRow DimcAsDataColumn ForEachrInrows ForEachcInr.Table.Columns Console.Write("/t{0}",r(c)) Nextc Console.WriteLine() Nextr EndSub

猜你在找的VB相关文章