To write records in DataTable just use WriteXml function
- String path = "C:\\TEMP\Customer.xml"
- dtCustomer.WriteXml( path );
To read xml file and store the records in DataTable, use ReadXml
- dtCustomer.ReadXml( path );
I can select which columns that will became primary key in DataTable
- DataColumn[] dcKeys = new DataColumn[1];
- dcKeys[0] = dtCustomer.Columns["Name"] ;
- dtCustomer.PrimaryKey = dcKeys;
DataTable.PrimaryKey accept array of DataColumn, that's why I must make dcKeys an array, eventhough I only use 1 column only. There are several ways to search certain record in DataTable, one of it is using DataView
- DataView dv = dtCustomer.DefaultView;
Please remember that if you use primary key in DataTable and want to create view from it, you must define sort column for DataView base on the primary key, I am not certain should we define all primary key column or we can just use some of it, or we can mix it with another column, try to figure it out later.
- dv.Sort = "Name, Address";
We can use DataView.FindRows, DataView.Find, or DataView.RowFilter to do searching. The differences between those 3 are, FindRows will return array of DataRowView meanwhile Find will return int that contain the row number where the record is. RowFilter will reindex the DataView so if you using it to often it will cost more resource. FindRows and Find need primary key to define, meanwhile RowFilter doesn't need it. RowFilter is used like where statement in SQL Query
- Object[] key = new Object[2]{ "silenr0c", "Bugis street" }
- int row = dv.Find( key );
- DataRowView drv = dv.FindRows( key );
- dv.RowFilter = "Name='Minamoto' or Name='silenr0c' ";
No comments:
Post a Comment