1 Database Programming with ADO.NET Kashef Mughal
2 Last Week We looked at the following: OleDbConnection - active connection to the database for an OLEDB provider DataSet - subset of the database, can be updateable OleDbDataAdapter - data bridge between the OleDbConnection and the DataSet for an OLEDB provider OleDbCommand - SQL command that is executed against the database
3 This Week We are going to work with the following: SqlConnection - active connection to the SQL Server database - carries the connection info SqlDataAdapter - data bridge between the SqlConnection and the DataSet for a SQL Server database SqlCommand - SQL command that is executed against the database which in this case is SQL Why different objects for SQL Server? These are optimized for retrieving data from SQL Server
4 A few thing about SQL Server Enterprise database from Microsoft Comes in versions 7, 2000 and YUKON (not out yet) Think of SQL Server as Access on steroids Enterprise Manager is the tool of choice for SQL Server administration Query Analyzer is the tool of choice for testing queries and SQL code Go ahead and open Enterprise Manager from Start - Program Files - Microsoft SQL Server - Enterprise Manager
5 Enterprise Manager Expand SQL Server Group Expand your Server Expand Database Expand Northwind Expand pubs Select tables Right click on Authors table and select Return all rows
6 Query Builder in EM Click on the Show/Hide Diagram pane - Second Tool Bar button Click on the Show/Hide Grid pane - Third Tool Bar button Click on the Show/Hide SQL pane - Fourth Tool Bar button Does this look familiar ? You can use this to build your SQL and then paste it in VB.NET
7 Connection String parameters Server - machine name Database - e.g. Northwind uid - username pwd - password Integrated Security - set to true if using Windows Authentication
8 Back to VB.NET sqlCommand represents a SQL command that can be executed against the Database Last week we used SELECT command Connection property of sqlCommand represents the connection to the database CommandText property of sqlCommand is the actual SQL command - Remember that this is a string so you will have to use double quotes Parameter Collection is used with parameters ExecuteNonQuery method with execute the SQL
9 sqlDataAdapter sqlDataAdapter is similar to OleDbDataAdapter in that it is a bridge between db and Data Set SelectCommand property is used to specify the SQL for sqlDataAdapter Fill is the method used to populate a Data Set using sqlDataAdapter
10 Data View object Works with the Data Set object Difference lies in the fact that Data View can be used for sorting and filtering data Sort property will sort the data RowFilter property is used to return only data with a specific criteria - similar to WHERE clause
11 Exercises Try the following ones Page 637 Page 641 Take a break when finished
12 The Server Explorer Window The Server Explorer can help you quickly create data driven applications as well as allow you to create SQL databases. The Server Explorer has two nodes at the top: Data Connections and Servers. The Data Connections node lists the databases that you've created a connection to using the data adapter wizard. The Servers node lists the db servers available to you.
13 Server Explorer Screen Shot
14 Advantages of Server Explorer Database Administration - You can not only view the objects but can create new ones - You may not have access to Enterprise Manager and can use this tool instead Can create multiple connections from the same window Monitor events and counters on your machine or other servers Minimize the code you need to write as shown in the next section
15 Advantages of Server Explorer - Part 2 We will be using pubs database on the SQL Server Start a new Project in Vb.NEt Drag the authors table from the Server explorer to the form Notice it created a connection and sql adapter Right click on sqldataadapter1 and Preview Data Right click and select Generate DataSet (New) Add a Data Grid to the form and set properties Add the code to the Form Load event
16 DATA FORM WIZARD The data form wizard allows you to quickly create a data bound form complete with the appropriate ADO.NET objects and bound controls. To use the wizard add a new item to your project by right clicking on the application name and choosing Add-->Add New Item This will start the wizard Pick new data set option, call it myDS - Next Use existing SQL connection - Next
17 DATA FORM WIZARD - Part 2 Select the employee table - Next Make sure all the fields are selected - Next How do you want to display data - Pick single record - Leave all the options check and select Finish Wow - You are done Make sure you have this DataForm1 selected as default Run the bad boy!
18 A few more terms Data Binding is the term used when you are using bound data control like a data grid Binding Context object manages all the bindings of the controls on a form It does this my managing a collection of currencymanager objects Currency Manager is responsible for keep data consistent between objects and data source
19 Exercises Try the following ones Page 647 Page 659 Page 661 Take a break when finished Remember next week is Final
20 Final Chapter 8-16 minus Chapter 13 In-class material Open Book, open notes 100 questions true/false multiple choice a few from the midterm 200 points 2 hours Grades right now