1 / 21

ADO.NET Continued

ADO.NET Continued. DataSets & DataAdapters. SqlDataReader provides stream-based access to the results of database queries Read-only, forward-only Could be an inconvenience ADO.NET provides a set-based access as well as a stream-based access. DataSets & DataAdapters.

Download Presentation

ADO.NET Continued

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ADO.NET Continued CS360 Windows Programming

  2. DataSets & DataAdapters • SqlDataReader provides stream-based access to the results of database queries • Read-only, forward-only • Could be an inconvenience • ADO.NET provides a set-based access as well as a stream-based access CS360 Windows Programming

  3. DataSets & DataAdapters • DataSet: in-memory database • SqlDataAdapter: bridge between real database and datasets CS360 Windows Programming

  4. DataSet • DataTable objects are analogous to tables in a database • DataSet.Tables property lists DataTables • There are also DataRow and DataColumn objects • Ideal for capturing the result of database queries and modifying the data • Useful for caching. How? CS360 Windows Programming

  5. DataSets vs. DataReaders • Which is better? • It depends CS360 Windows Programming

  6. DataAdapter Classes • DataSets are mostly initialized from database queries or XML documents • DataSets do not interact with the database directly • DataAdapters perform database queries and create DataTables containing the query results • When working with Microsoft SQL Server, we are concerned with SqlDataAdapter CS360 Windows Programming

  7. Example SqlDataAdapter adapter = new SqlDataAdapter(“select * from titles”, “server=localhost;database=pubs;uid=sa; pwd=“); DataSet ds = new DataSet(); Adapter.Fill(ds, “Titles”); CS360 Windows Programming

  8. What Happens? • Fill opens a connection to the pubs database using the connection string • It performs a query using the query string • It creates a DataTable named “Titles” in the DataSet • It initializes the DataTable with a schema that matches “titles” in database • It retrieves all records produced by the query and writes them to the DataTable • It closes the connection to the database CS360 Windows Programming

  9. DataTable and Friends • A DataSet is a collection of DataTables foreach(DataTable table in ds.Tables) Console.WriteLine(table.TableName); CS360 Windows Programming

  10. DataTable and Friends • Individual DataTables can be referenced by name or index • DataTables contain DataRows DataTable table = ds.Tables[0]; foreach(DataRow row in table.Rows) Console.WriteLine(row[0]); • Could also use column name instead of index CS360 Windows Programming

  11. DataTables • You can also enumerate a DataTable’s columns DataTable table = ds.Tables[0]; foreach(DataColumn col in table.Columns) Console.WriteLine(“Name={0}, Type={1}”,col.ColumnName, colDataType); CS360 Windows Programming

  12. Adding Rows to a Table • Perform a query with DataAdapter.Fill, add records to the resulting DataTable, and write the changes to the database SqlDataAdapter adapter = new SqlDataAdapter ("select * from titles", "server=localhost;database=pubs;uid=sa;pwd="); DataSet ds = new DataSet (); adapter.Fill (ds, "Titles"); DataTable table = ds.Tables["Titles"]; DataRow row = table.NewRow (); row["title_id"] = "JP1001"; row["title"] = "Programming Microsoft .NET"; row["price"] = "59.99"; row["ytd_sales"] = "1000000"; row["type"] = "business"; row["pubdate"] = "May 2002"; table.Rows.Add (row); CS360 Windows Programming

  13. Searching Records in a DataTable • Use Select DataRow[] rows = table.Select ("title_id = 'JP1001'"); DataRow[] rows = table.Select ("price < 10.00"); // Return all rows where "State" equals CA, TN, or WA DataRow[] rows = table.Select ("state in ('ca', 'tn', 'wa')"); // Return all rows where "State" begins with CA DataRow[] rows = table.Select ("state like 'ca*'"); CS360 Windows Programming

  14. Updating Records in a DataTable SqlDataAdapter adapter = new SqlDataAdapter ( "select * from titles", "server=localhost;database=pubs;uid=sa;pwd="); DataSet ds = new DataSet (); adapter.Fill (ds, "Titles"); DataRow[] rows = table.Select ("ytd_sales > 10000"); foreach (DataRow row in rows) row["price"] = (decimal) row["price"] + 10.00m; CS360 Windows Programming

  15. Deleting Records from a Table SqlDataAdapter adapter = new SqlDataAdapter ( "select * from titles", "server=localhost;database=pubs;uid=sa;pwd="); DataSet ds = new DataSet (); adapter.Fill (ds, "Titles"); DataRow[] rows = table.Select ( "ytd_sales < 10000 OR isnull (ytd_sales, 0) = 0"); foreach (DataRow row in rows) row.Delete (); CS360 Windows Programming

  16. Propagating Changes • Inserts, updates, and deletes performed on a DataTable do not automatically propagate back to the database CS360 Windows Programming

  17. SqlDataAdapter adapter = new SqlDataAdapter ("select * from titles", "server=localhost;database=pubs;uid=sa;pwd="); SqlCommandBuilder builder = new SqlCommandBuilder (adapter); DataSet ds = new DataSet (); adapter.Fill (ds, "Titles"); DataTable table = ds.Tables["Titles"]; DataRow row = table.NewRow (); row["title_id"] = "JP1001"; row["title"] = "Programming Microsoft .NET"; row["price"] = 59.99m; row["ytd_sales"] = 1000000; row["type"] = "business"; row["pubdate"] = new DateTime (2002, 5, 1); table.Rows.Add (row); adapter.Update (table); CS360 Windows Programming

  18. Update Only Changes • Could specify that only changes be updated // Update the database DataTable delta = table.GetChanges (); adapter.Update (delta); • Not necessary • Update is smart enough CS360 Windows Programming

  19. CommandBuilder • How does Update physically update the database? • Executes SQL commands • INSERT, UPDATE, DELETE • Manufactured by SqlCommandBuilder CS360 Windows Programming

  20. DataView SqlDataAdapter adapter = new SqlDataAdapter ("select * from titles", "server=localhost;database=pubs;uid=sa;pwd="); DataSet ds = new DataSet (); adapter.Fill (ds, "Titles"); DataView view = new DataView (ds.Tables["Titles"]); view.Sort = "title ASC"; MyDataGrid.DataSource = view; MyDataGrid.DataBind (); CS360 Windows Programming

  21. Summary • Completed Chapter 12 • Next Time • XML • Multithreading CS360 Windows Programming

More Related