Aug 2, 2007

Dataset Interview

Q:What is dataset ?
A:Dataset is a database on ram.
dataset contain number of datatable.
datatable contain number datacolumns and datarow collection.
Each datatable may have a primary key and foreign key and have a data relation to other datatable.
Each datacolumn have datatype.
Each datarow contain record of datatable.
dataset itself not know about source of data.
data to fill in dataset may from many kind of RDBMS,textfile,XML file, of just create data in memory from code.

Q:How i add new row to datatable ?
A: You just simple create new row a reference to a NewRow() method of datable.
new row is a depend from datatable while you not add it.
When you add It.It will add to last row of datatable.
'sample code
dim DR as DataRow = DS.tables("Customer").NewRow()

'set each field
DR("CustomerName") = "John doo"
DR("CusPhone")= "555-555-555"


Q:How DS.Tables("Mytable").Clear different from DS.Tables.Remove("MyTable")
A: Clear() method of datatable make datable delete all row.but structure of datatable is not delete too.
in this case DS will contain datatable Mytable and Mytable will contain each datacolumns.It is similar Delete MyTable in T-SQL.

But Method Remove() of Dataset.Tables is remove datatable exctracly.It is similar DROP statement in T-SQL

Q:I have a datarow ,and i want to add to two datable.How i do that ?
A: you must use method ImportRow() of datatable instead .Rows.Add().

Q:How i retrive data from database to dataset ?
A:ADO.Net have many method to retrive data from database
but this is one of easiest method.

'you just create new DataAdapter and set SQL statement for retrive data do you want
'and set Connection object
'behind the scence DataAdapter will retrive data from SQL statement throught Connection object
'and then you just use Fill method ,first parameter is dataset object, second is table name do you want
dim DA as New Oledb.OledbDataAdapter("Select * from Customer",conObj)

Q:How i update data from dataset to database ?
A:This solution similar when you want to retrive data from database
dim DA as New Oledb.OledbDataAdapter("Select * from Customer",conObj)
dim CB as New Oledb.OledbCommandBuilder(A)' you just add this line for create Command builder and reference to DataAdapter
DA.Update (DS,"Customer") ' and then update dataset table Customer throught Update method

Behind the scence CB object will create Insert,Update,Delete SQL statement from schema
if you serious performance issue you should create SQL statement by manual
This example just show you how to update by simplest way.

Q:Are datatable must retrive data from database ?
A:NO. Datatable can create datafrom void.
you can create new datatable and create new datacolumn.and then you use it with out database.
dim DT as new Datable

DT.DataColumns.Add(new DataColumn("ID",type.GetType(System.String)))
DT.DataColumns.Add(new DataColumn("Name",type.GetType(System.String)))

'and then you may add a datarow from Initialvalue of datatable

Q:How i use Dataset read XML file ?
A:Yes. You just use ReadXML() Method from Dataset and you can Save XML data from dataset to XML file,just use WriteXML() Method.

Q:Why i can retrive data from database to dataset and i can insert new data too.But I can't Update or delete data ?
A:May your table in database don't set Primary key.When you use dataadapter for update.It must you Primary Key for generate SQL statement.
In case of select or insert ,it not have problem but in case of update or delete it will crash.

No comments: