Beruflich Dokumente
Kultur Dokumente
home
articles
quick answers
discussions
features
community
help
Next
Articles Desktop Development Grid & Data Controls DataSets, DataGrids etc
Article Browse Code Stats Revisions Alternatives Comments & Discussions (39)
About Article
The purpose of this document is to provide a practical guide to using Microsofts .NET DataTables, DataSets and DataGrid Type Licence First Posted Views Bookmarked 23 Feb 2004 251,356 142 times Article
A c c e p t C h a n g e s ( )
C l e a r ( )
C l o n e ( )
C o p y ( )
G e t C h a n g e s ( )
H a s C h a n g e s ( )
W r i t e X m l ( )
R e a d X m l ( )
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D
1/9
1/20/14
C a s e S e n s i t i v e
If set to true then string compares in DataSet tables are case sensitive otherwise they are not. Name of the DataSet Returns true if there are any errors within any tables in the DataSet Collection of Relations Method/PropertyDescription
Top News
Programmers without TDD will be unemployable by 2022
Get the Insider News free each morning.
D a t a S e t N a m e
H a s E r r o r s
Related Videos
A d d ( )
If two or more relations are to be added to the collection they can be added using this method. They are appended to the existing collection in the order specified in the range. Returns true if the relation can be removed from the collectionn Removes all relations from the relations collection Returns a true if the collection contains the named relation. Returns the number of relations in the collection Returns the index of a relation in the collection equal to name. Removes a relation by Name from the collection Removes a relation by index from the collection.
A d d R a n g e ( )
C a n R e m o v e
Related Articles
Matrix Multiplication in C# Creating animations with Dundas Chart for ASP.NET Smarter Data Labels with Dundas Chart SmartLabels Understanding Chart Areas with Dundas Chart for .NET A Formatted Text Box Using screensavers inside the Windows Media Player Making Sense of Geographic Data with Dundas Map and AJAX Handling connection notification between a desktop machine and Windows CE based devices Create data-driven applications with the Hera Application Framework Towards the self-documenting database: extended properties Accessibility audit vs. accessibility testing Digital Signatures and PDF Documents Color Scale Filter Merge Landscape and Portrait PDFs using ASP.NET Using Barcodes in Documents Best Practices How to Retrieve EMC Centera Cluster/Pool Capabilities Embedding IronPython in WPF Using C# "Hey! Is That My Car? How to Sharpen a QuickBird Satellite Image Using DotImage" Integrate your SharePoint environment into the open standards-based WebSphere Portal platform using the Visual Studio IDE
R e l a t i o n s s
C l e a r ( )
C o n t a i n s ( )
C o u n t
I n d e x O f ( )
R e m o v e ( )
R e m o v e A t ( )
A d d ( )
Adds a table to the collection If two or more tables are to be added to the collection they can be added using this method. They are appended to the existing collection in the order specified in the range. Removes all tables from the collection Returns a true if the collection contains a table with TableName equal to name.
A d d R a n g e ( )
C l e a r ( ) T a b l e s
C o n t a i n s ( )
2/9
1/20/14
C o u n t
Returns the number of tables in the collection Returns the index of a table in the collection with a TableName equal to name. Removes a table by TableName from the collection
VBScript / Excel 2007 - An easy way to access DBF files Retrieving and Storing Call History
I n d e x O f ( )
Related Research
R e m o v e ( )
R e m o v e A t ( )
/ /A d dt h eE l e m e n t st a b l et ot h eD a t a S e t e l e m e n t D S . T a b l e s . A d d ( d t E l e m e n t s ) ; / /A d dt h eI s o t o p e st a b l et ot h eD a t a S e t e l e m e n t D S . T a b l e s . A d d ( d t I s o t o p e s ) ;
Method 2 T a b l e s . A d d R a n g e ( )
Collapse | Copy Code
E l e m e n t D S . T a b l e s . A d d R a n g e ( n e wD a t a T a b l e ( ) { d t E l e m e n t s ,d t I s o t o p e s } ) ;
The following code illustrates how to directly load or bind a database recordset using the sqlDataAdapters F i l l ( ) method where the select query string was used to create the recordset. After the Fill() method is called, ds will contain a table in its collection with column headers that match the field names in the select query string and column datatypes will match those specified in the database table elements. Each row will contain data corresponding to each field.
Collapse | Copy Code
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D
3/9
1/20/14
s t r i n gS Q L =" S E L E C TE l e m e n t s I D ,A t o m i c N b r ,"+ " S y m b o l ,A t o m i c M a s s ,E l e m e n t +F R O M[ E l e m e n t s ]o r d e rb yA t o m i c N b rA S C " ; s q l C o n n e c t i o ns q l C o n n e c t i o n 1 = n e ws q l C o n n e c t i o n( " c o n n e c t i o ni n f ot oM S D Eo rS Q LS e r v e r2 0 0 0 + ; ) ; s q l C o m m a n ds q l C o m m a n d=n e ws q l C o m m a n d( S Q L , s q l C o n n e c t i o n 1 ) ; s q l C o n n e c t i o n 1 . O p e n ( ) ; s q l D a t a R e a d e re l e m e n t R e a d e r=S q l C o m m a n d . E x e c u t e R e a d e r ( ) ; / /S t a r t i n gw i t ht h ee l e m e n tt a b l ed td e f i n e di n / /t h eT a b l e ss e c t i o n ,a nE l e m e n t I Dc o l u m n / /i sa d d e dt h a tw i l lb eu s e da st h ep r i m a r yk e yf o rt h er o w . D a t a C o l u m nd c=n e wD a t a C o l u m n ( E l e m e n t s I D , S y s t e m . T y p e . G e t T y p e ( S y s t e m . G u i d ) ) ; d t . C o l u m n s . A d d ( d c ) ; / /M a k e E l e m e n t s I D ap r i m a r yk e y : d t . P r i m a r y K e y=n e wD a t a C o l u m n [ ] { d t . C o l u m n s [ " E l e m e n t s I D " ] } ; / /F i l lt a b l ed tw i t hd a t af r o mt h ed a t a b a s et a b l eE l e m e n t s :
Note: the sqlDataReader class has a method that can be used to determine whether a null or non-existent value was returned for a particular cell. For example:
Collapse | Copy Code
This check has been omitted in the following code for clarity, but it is a good practice to use it.
Collapse | Copy Code
D a t a R o wd r ; i n tP r e v A t o m i c N b r=0 ; t r y { w h i l e ( m y R e a d e r . R e a d ( ) ) { i f( P r e v A t o m i c N b r! =e l e m e n t R e a d e r . G e t I n t 3 2 ( e l e m e n t R e a d e r . G e t O r d i n a l ( " A t o m i c N b r " ) ) ) { P r e v A t o m i c N b r=e l e m e n t R e a d e r . G e t I n t 3 2 ( e l e m e n t R e a d e r . G e t O r d i n a l ( " A t o m i c N b r " ) ) ; d r=d t . N e w R o w ( ) ; d r [ E l e m e n t s I D ]= e l e m e n t R e a d e r . G e t G u i d ( e l e m e n t R e a d e r . G e t O r d i n a l ( " E l e m e n t s I D " ) ) ; d r [ A t o m i c N b r ]=e l e m e n t R e a d e r . G e t I n t 3 2 ( e l e m e n t R e a d e r . G e t O r d i n a l ( " A t o m i c N b r " ) ) ; d r [ S y m b o l ]=e l e m e n t R e a d e r . G e t S t r i n g ( e l e m e n t R e a d e r . G e t O r d i n a l ( " S y m b o l ) ) ; d r [ E l e m e n t ]=e l e m e n t R e a d e r . G e t S t r i n g ( e l e m e n t R e a d e r . G e t O r d i n a l ( " E l e m e n t " ) ) ; d r [ A t o m i c M a s s ]= e l e m e n t R e a d e r . G e t D e c i m a l ( e l e m e n t R e a d e r . G e t O r d i n a l ( " A t o m i c M a s s " ) ) ; d t . R o w s . A d d ( d r ) ; } } } f i n a l l y { e l e m e n t R e a d e r . C l o s e ( ) ; s q l C o n n e c t i o n 1 . C l o s e ( ) ; } d t . A c c e p t C h a n g e s ( ) ; / /A d dt a b l ed tt oan e wd a t a s e td sa n di t st a b l e sc o l l e c t i o n D a t a S e td s=n e wD a t a S e t ( ) ; d s . T a b l e s . A d d ( d t ) ;
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D
4/9
1/20/14
/ /c r e a t ean e wD a t a S e tn a m e dP e r i o d i ct h a tw i l l / /h o l dt w ol i n k e dt a b l e sw i t h / /T a b l e N a m e so fE l e m e n t sa n dI s o t o p e sr e s p e c t i v e l y . D a t a S e te l e m e n t D S=n e wD a t a S e t ( " P e r i o d i c " ) ; / /C r e a t eE l e m e n t sT a b l e D a t a T a b l ed t E l e m e n t s=n e wD a t a T a b l e ( " E l e m e n t s " ) ; d t E l e m e n t s . C o l u m n s . A d d ( " A t o m i cN u m b e r " ,t y p e o f ( i n t ) ) ; d t E l e m e n t s . C o l u m n s . A d d ( " E l e m e n t " ,t y p e o f ( s t r i n g ) ) ; d t E l e m e n t s . C o l u m n s . A d d ( " S y m b o l " ,t y p e o f ( s t r i n g ) ) ; / /M a k e A t o m i cN u m b e r ap r i m a r yk e yi nE l e m e n t st a b l e d t E l e m e n t s . P r i m a r y K e y=n e wD a t a C o l u m n [ ] { d t E l e m e n t s . C o l u m n s [ " A t o m i cN u m b e r " ] } ; / /C r e a t eI s o t o p e sT a b l e D a t a T a b l ed t I s o t o p e s=n e wD a t a T a b l e ( " I s o t o p e s " ) ; d t I s o t o p e s . C o l u m n s . A d d ( " S y m b o l " ,t y p e o f ( s t r i n g ) ) ; d t I s o t o p e s . C o l u m n s . A d d ( " A t o m i cN u m b e r " ,t y p e o f ( i n t ) ) ; d t I s o t o p e s . C o l u m n s . A d d ( " I s o t o p eN u m b e r " , t y p e o f ( i n t ) ) ; d t I s o t o p e s . C o l u m n s . A d d ( " P e r c e n tA b u n d a n c e " , t y p e o f ( S y s t e m . D e c i m a l ) ) d t I s o t o p e s . C o l u m n s . A d d ( " A t o m i cM a s s " ,t y p e o f ( S y s t e m . D e c i m a l ) ) ; / /A d dt a b l e st ot h eD a t a s e t E l e m e n t D S . T a b l e s . A d d R a n g e ( n e wD a t a T a b l e ( ) { d t E l e m e n t s ,d t I s o t o p e s } ) ;
Dataset . R e l a t i o n s . A d d ( )
Collapse | Copy Code
/ /A d dar e l a t i o n s h i pf o rT a b l e sE l e m e n t sa n dI s o t o p e s / /t h r o u g ht h ep r i m a r yk e y A t o m i cN u m b e r i nD a t a S e t / /P e r i o d i ca n dn a m et h er e l a t i o n s h i p I s o t o p e s .T h i sn a m ei s / /u s e di nt h eD a t a G r i dt os e l e c tt a b l er o w s ,i nt a b l eI s o t o p e s , / /t h a tc o n t a i ni s o t o p ev a l u e sf o rt h es e l e c t e de l e m e n t . e l e m e n t D S . R e l a t i o n s . A d d ( " I s o t o p e s " , e l e m e n t D S . T a b l e s [ " E l e m e n t s " ] . C o l u m n s [ " A t o m i cN u m b e r " ] , e l e m e n t D S . T a b l e s [ " I s o t o p e s " ] . C o l u m n s [ " A t o m i cN u m b e r " ]) ;
/ /A s s i g nt h et a b l ec o n t a i n i n gb o t hE l e m e n t sa n d / /I s o t o p e st od tu s i n gi n d e x0 D a t a T a b l ed t=d s . T a b l e s [ 0 ] ; / /C r e a t et w oD a t a T a b l ev a r i a b l e sd t E l e m e n t sa n dd t I s o t o p e sa n d / /a s s i g nt a b l e sc o n t a i n e di nt h eD a t a S e te l e m e n t D ST a b l e s / /c o l l e c t i o nu s i n gT a b l e N a m e sa si n d e x e s . D a t a T a b l ed t E l e m e n t s=e l e m e n t D S . T a b l e s [ E l e m e n t s ] ; D a t a T a b l ed t I s o t o p e s=e l e m e n t D S . T a b l e s [ I s o t o p e s ] ; D a t a R o wd r E l e m e n t ; D a t a R o wd r I s o t o p e ; i n tp r e v A t o m i c N b r=0 ; f o r e a c h( D a t a R o wd ri nd t . R o w s ) { i f ( p r e v A t o m i c N b r! =( i n t ) d r [ " A t o m i c N b r " ] ) {/ / n e e do n l yo n er o wp e rA t o m i c N b ri nT a b l e [ E l e m e n t s ] / /F i l la ne l e m e n tr o ww i t hd a t af r o md t . p r e v A t o m i c N b r=( i n t ) d r [ " A t o m i c N b r " ] ; d r E l e m e n t=d t E l e m e n t s . N e w R o w ( ) ; d r E l e m e n t [ " A t o m i cN u m b e r " ]=d r [ " A t o m i c N b r " ] ; d r E l e m e n t [ " E l e m e n t " ]=d r [ " E l e m e n t " ] ; d r E l e m e n t [ " S y m b o l " ]=d r [ " S y m b o l " ] ; d t E l e m e n t s . R o w s . A d d ( d r E l e m e n t ) ;
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D
5/9
1/20/14
4.5.2 Removing
To remove all linked tables or a selected table that is linked from the D a t a S e t , it is first necessary to remove all relations, then constraints and then the table otherwise relationship/constraint table errors are generated. The following code example provides a generic routine for removing all linked tables in a dataset.
Collapse | Copy Code
p u b l i cv o i dR e m o v e A l l T a b l e s ( D a t a S e td s ) { / /n e e dt od oi ti nr e v e r s eo r d e rd u et oc o n s t r a i n t s d s . R e l a t i o n s . C l e a r ( ) ; f o r( i n ti = d s . T a b l e s . C o u n t1 ;i> = 0 ;i ) { d s . T a b l e s [ i ] . C o n s t r a i n t s . C l e a r ( ) ; d s . T a b l e s . R e m o v e A t ( i ) ; } }
d s . N a m e s p a c e=" h t t p : / / w w w . m y d o m a i n . c o m / x m l f i l e s d s . W r i t e X m l ( F i l e N a m e ,X m l W r i t e M o d e . W r i t e S c h e m a ) ;
4.6.2 R e a d X m l
All tables with their schemas, relationships, constraints and data contained in an XML file are imported into a DataSet by specifying a DataSet property Namespace and using the R e a d X m lmethod. Once in the D a t a S e tit is just like any other dataset. For example:
Collapse | Copy Code
i f ( d s . H a s E r r o r s ) {/ /O n eo rm o r eo ft h et a b l e si nt h eD a t a S e th a se r r o r s . M e s s a g e B o x . S h o w ( " D a t a S e th a sE r r o r s " ) ; / /I n s e r tc o d et or e s o l v ee r r o r s . / /R e f e rt o H a n d l i n gD a t a T a b l eE r r o r s s e c t i o nf o re x a m p l eo f / /h a n d l i n ge r r o r sw i t h i nt a b l e s . }
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D
6/9
1/20/14
Next
Data Grids
License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below. A list of licenses authors might use can be found here
Pete2004
President Bioxing United States Ph.D. in BioPhysics and over 20 years of experience in managing, architecting and hands-on developing software systems for biotechnology companies that produced cutting edge instrumentation and data systems. These include DNA and Peptide Synthesizers, cDNA, oligo and Protein microarrays and mass spectrometers used for protein sequencing.In 2001 founded BioXing (pronounced Bio-Crossing) which has architected and developed an extensible relational database repository and software system that includes Web Services, Client Workstation and Web Based applications. The system is used to track, manage, integrate and data mine disparate laboratory data, protocols and experiments and link to reference proteomic and genomic data. BioXing also does consulting and development for biotechnology companies. Article Top
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D
7/9
1/20/14
My vote of 5 Good article but unnecessary complex [Message Removed] Re: Good article but unnecessary complex Updating Dataset Problems Re: Updating Dataset Problems Re: Updating Dataset Problems Re: Updating Dataset Problems [modified] Re: Updating Dataset Problems how read a xml with dataset class Re: how read a xml with dataset class InsertAt() causes errors Re: InsertAt() causes errors conversion excel file into txt file WriteXML and blob Re: WriteXML and blob Programatic DataGrid Scrolling Re: Programatic DataGrid Scrolling Re: Programatic DataGrid Scrolling Re: Programatic DataGrid Scrolling ReadXML and datagrid Re: ReadXML and datagrid A problem with WriteXml Re: A problem with WriteXml Re: A problem with WriteXml Changing Column Order Re: Changing Column Order Re: Changing Column Order Re: Changing Column Order Re: Changing Column Order Re: Changing Column Order
Bolud pentel immetoz Pete2004 HinJinShah Pete2004 HinJinShah Pete2004 HinJinShah rjorget DharmarajNagarajan Mr.Bill Pete2004 Miroka Holger Flick Pete2004
13-Aug-10 12:14 27-Aug-08 19:45 4-Oct-08 2:02 6-Oct-08 6:04 1-Aug-07 5:50 1-Aug-07 6:04 3-Aug-07 9:38 4-Aug-07 14:56 8-Aug-07 6:43 2-Feb-06 8:13 13-Nov-07 23:18 16-Aug-05 2:13 17-Aug-05 10:45 8-Jun-05 7:06 31-Jul-04 13:21 17-Aug-05 10:48 10-Jul-04 9:00 10-Jul-04 13:09 15-Jul-04 19:32 15-Jul-04 6:34 28-May-04 11:34 28-May-04 12:07 17-May-04 6:27 17-May-04 7:08 17-May-04 11:18 4-May-04 3:32 5-May-04 4:33 5-May-04 4:40 5-May-04 5:40 5-May-04 5:45 5-May-04 5:55
Anonymous
partyganger
Anonymous
Pete2004
Anonymous
Pete2004 sjkdo Pete2004 sjkdo EstimatedProphet Pete2004 EstimatedProphet Pete2004 EstimatedProphet Pete2004
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D
8/9
1/20/14
Anonymous
dothedew Boyan Botev Pete2004 Pete2004 MikeBeard Pete2004
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Mobile Web02 | 2.8.140119.1 | Last Updated 24 Feb 2004 Layout: fixed | fluid Article Copyright 2004 by Pete2004 Everything else Copyright CodeProject, 1999-2014 Terms of Use
www.codeproject.com/Articles/6180/A-Practical-Guide-to-NET-DataTables-DataSets-and-D
9/9