Using DataSet Objects

Questions That Should Be Asked More Frequently

  1. Q. Do I need to use a DataSet if I want to work with only a few rows of data?

  2. A. In this situation, you can use just a DataTable object without using a DataSet. The DataSet offers support for relations between tables, the ability to read and write data to files or streams, and support for XML features. If you don't need to use any of these features, you can use just a DataTable.

  3. Q. I added a row to my DataTable, and when I submitted the new row to my database, I received an error that said the new row violated the primary key constraint on the table. Why didn't I receive this error when I added the row to the DataTable?

  4. A. ADO.NET enforces the constraints you create based on the data in your DataSet. The new row that you created will not violate the primary key constraint on your DataTable unless the DataTable already contains a row with the same values in the primary key columns. ADO.NET does not have any inherent knowledge of what data exists in your database.

  5. Q. How do I examine the contents of a deleted row?

  6. A. Use DataRowVersion.Original for the optional parameter on the Item property.

  7. Q. Is there any way to undo the changes I've made to my rows?

  8. A. Yes. You can call the RejectChanges method on a DataRow, DataTable, or DataSet to discard any pending changes in the affected rows.

  9. Q. How can I tell whether a DataTable has a primary key defined?

  10. A. You can use the following snippet of code:

    Visual Basic .NET

    If tbl.PrimaryKey.Length > 0 Then     'DataTable has a primary key. Else     'DataTable has no primary key. End If

    Visual C# .NET

    if (tbl.PrimaryKey.Length > 0)     //DataTable has a primary key. else      //DataTable has no primary key.

  11. Q. What of DataSet objects and the Dispose method?

  12. A. The DataSet, DataTable, and DataColumn objects are each derived from the MarshalByValueComponent class, which exposes both a Dispose method and a Disposed event. You can use the Dispose method to release an object's resources. You can also trap for an object's Disposed event if you want to execute code when the object's Dispose method executes.

    If you're going to call the Dispose method on any of these objects, it's worth noting that the effect of their Dispose method is not recursive. Calling the Dispose method of a DataSet object does not implicitly call the Dispose method of the DataTable objects of the DataSet.



Microsoft ADO. NET Core Reference
Microsoft ADO.NET (Core Reference) (PRO-Developer)
ISBN: 0735614237
EAN: 2147483647
Year: 2002
Pages: 104
Authors: David Sceppa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net