home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / adoplus / getdatafromdb.aspx < prev    next >
Encoding:
Text File  |  2000-06-09  |  4.1 KB  |  55 lines

  1.  
  2. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  3.  
  4. <!-- #include virtual="/quickstart/include/header.inc" -->
  5.  
  6. <h4>ADO+:Get Data from a Database</h4>
  7. <p>
  8. <hr>
  9. Getting data from a database is easy, and working with data is easier and better than before. This section describes how to use a SQLCommandSet to get data from a database into a DataSet.  
  10. <p>
  11. The most important concept to remember is that the DataSet is a data structure separate and distinct from a data store. Although in this example we get data from a database, it doesn't matter where the data comes from; the DataSet will always present a consistent programming model. It is a simple collection of data with relational database characteristics. There is no Load, Open or Execute on a DataSet because it doesn't know where it gets its data from.
  12. <p>
  13. You can add data to the DataSet using the Add method (see <a href="UpdateDataFromDB.aspx">Updating Data in a Database</a>) for more details), or directly push data in with the intrinsic XML methods (see <a href="readandwritexml.aspx">Reading and Writing XML Data</a>); SQLDataSetCommand is yet another tool: it can also push data back into the database. Here we focus on getting data out.
  14. <p>
  15. First we construct a SQLDataSetCommand. This is done in the same way a SQLCommand is constructed. 
  16. <P>
  17. <div class=code>
  18. SQLDataSetCommand CustomersDSCommand = new SQLDataSetCommand("select * from customers", myConnection);
  19. </div>
  20. <p>
  21. A SQLDataSetCommand is much like a SQLCommand with a few specific differences. Most significantly, the FillDataset and Update methods. The FillDataset method populates a DataSet. The Update method takes changes from a DataSet and pushes them back into the database. This is accomplished by placing four commands on the CommandSet (thus, the set of commands). These commands are: SelectCommand, UpdateCommand, InsertCommand and DeleteCommand.  These commands can be constructed for you at run-time (you need only set the SelectCommand and the others will be set on demand), you can easily take more control of these.  For example, if you use different stored procedures for Insert, Update, Delete, and so on you can establish this pattern in your SQLDataSetCommand. 
  22. <p>
  23. **NOTE**: Visual Studio adds great value to establishing typed SQLDatasetCommands and DataSets, and eventually creates Stored Procedures for you. Explore this feature by using the Component designer and Database Objects.
  24. <p>
  25. Once your SQLDataSetCommand is established you can pass it a DataSet to populate:
  26.  
  27. <div class=code>
  28. <br>    dsCustomer = new DataSet();
  29. <br>
  30. <br>    CustomersDSCommand.FillDataSet(dsCustomer,"Customers");
  31. </div>
  32. <p>
  33. The DataSet now holds the results of your query. In fact, the DataSet can hold results from multiple queries and even relate them (see <a href="relationaldata.aspx">Relational Data</a>). Because it holds multiple results, the DataSet contains a collection of tables. Notice that the FillDataSet method has "Customers" as the second argument. This is the name of the table to fill in the DataSet. If that table does not exist, it is created for you.
  34. <p>
  35. Because the data is stored in a collection of rows in the table, you can easily use For Each statement to iterate through the rows:
  36. <p>
  37. <div class=code>
  38. <br>    foreach( DataRow Customer in dsCustomer.Tables["Customers"].Rows)
  39. <br>    {    
  40. <br>    Response.Write(Customer.Column["Name"].ToString() );
  41. <br>    }
  42. </div>
  43. <p>
  44. In fact, you can use For Each over the columns as well. Below is an example placing together all of the code in this document. To learn how to update this data back into the database see <a href="updatedatafromdb.aspx">Updating Data in a Database</a>.
  45. <p>
  46. <Acme:SourceRef 
  47.   RunSample="/quickstart/howto/samples/adoplus/cs/gettingdata.aspx" 
  48.   ViewSource="/quickstart/howto/samples/adoplus/cs/gettingdata.src"
  49.   Icon="/quickstart/images/genicon.gif"
  50.   Caption="gettingdata.aspx"
  51.   runat="server" />
  52. </a>
  53. <p>
  54. Note: Anytime a SQLDatasetCommand, SQLConnection or SQLCommand is referenced, the same API is generated on the corresponding ADO versions that work through OLEDB.
  55.