Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.

Similar presentations


Presentation on theme: "Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load."— Presentation transcript:

1 Chapter 11 Introduction to Database Processing

2 Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load database data into the project, and edit that data using control instances created on a form Perform specialized database processing tasks beyond those performed by the Data Source Configuration Wizard Work with database data programmatically

3 Naming Database Tables and Fields Standard (Hungarian) prefixes are commonly used to name database tables and fields –The prefix "tbl" denotes a table –The prefix "fld" denotes a field

4 SQL Statements (Example) Select all rows from the table named tblCustomers, sorting the records by the fields named fldLastName and fldFirstName SELECT * FROM tblCustomers ORDER BY fldLastName, fldFirstName

5 ADO.NET (Introduction) Database management in.NET is performed through ActiveX Data Objects (ADO.NET) –The System.Data and System.Data.OleDb namespaces make up ADO.NET

6 Steps to Working with ADO.NET First, a database connection is established Second, an SQL command is sent over the open connection Third, ADO.NET builds an in-memory representation of the returned data Fourth, the database connection is closed Optionally, changes can be made to the in- memory representation of the data Finally, changes can be propagated back to the database

7 Visual Studio Database Wizards (Introduction) Database processing can get complex Visual Studio supplies the Data Source Configuration Wizard to perform the following tasks: –Create a database connection based on information you specify –Select the tables and fields that will be included in the data source

8 Understanding the Concept of a Data Source A data source is a connection between a Visual Studio project and a database A data source can be configured to connect to different types of databases The Data Sources window is used to manage an application's data sources

9 Figure 11-2: Connecting to a Database

10 Project Data Sources A project can have one or many data sources Each data source appears in the Data Sources window Use the Data Sources window to create new data sources and modify existing ones

11 Figure 11-3: Data Sources Window

12 Creating and Configuring a Database Connection Use the Data Source Configuration Wizard to create a new data source –The Data Source Configuration Wizard creates a connection string A connection string is used by ADO.NET to establish the database connection The Data Sources window displays the tables and fields in a data source

13 Figure 11-4: Data Sources Window with a Table and Fields

14 Creating Bound Control Instances Drag fields from the Data Sources window to the Windows Forms Designer –The control instances are configured and bound automatically –The control type is based on the data type of the database field TextBox control instances are created for String fields CheckBox control instances are created for Boolean fields The default control type can be changed

15 Figure 11-5: Control Instances Bound to a DataSet

16 Configuring a Data Source Click Data, Show Data Sources to activate the Data Sources window Click the link to begin creating a new data source

17 Figure 11-6:Data Sources Window with No Configured Data Sources

18 Figure 11-7: Data Source Configuration Wizard – Choose a Data Source Type

19 Creating a Database Connection Use the Data Source Configuration Wizard to create a new connection Create a connection to either a Microsoft Access Database File or an SQL Server database –Supply authentication information, as necessary Test the connection Add the connection to the project Save the connection string to the Application Configuration File

20 Figure 11-8: Data Source Configuration Wizard – Choose Your Data Connection

21 Figure 11-9: Add Connection Dialog Box

22 Figure 11-10: Change Data Source Dialog Box

23 Figure 11-11: Test Connection Message Box

24 Figure 11-12: Data Source Configuration Wizard Displaying the Connection String

25 Figure 11-13: Local Database File Message Box

26 Figure 11-14: Save the Connection String to the Application Configuration File

27 Selecting Database Objects Once the connection string has been created, the database objects can be selected Using the Data Source Configuration Wizard, select the desired tables and fields Assign a name to the DataSet

28 Figure 11-15: Data Source Configuration Wizard – Choose Your Database Objects

29 Creating Bound Control Instances The configured data source appears in the Data Sources window Drag fields from the Data Sources window to the Windows Forms Designer –The control instances are automatically bound to the data source –Prompts are automatically created

30 Figure 11-16: Windows Forms Designer Displaying Bound Control Instances at Design Time

31 Figure 11-17: Windows Form Displaying Bound Control Instances at Run Time

32 Actions Performed by the Data Source Configuration Wizard The Wizard creates a connection string over which commands are sent The Wizard creates a TableAdapter class –This class along with the OleDbDataAdapter send and receive data over the connection The Wizard creates a BindingSource class to navigate from record to record The Wizard creates a BindingNavigator class with which the end user interacts

33 Introduction to Connection Strings The Data Source Configuration Wizard creates a connection string –A connection string consists of key-value pairs –An equals sign separates a key and value –A semicolon separates a key-value pair Connection strings differ between database providers Example: Dim ConnectionString As String ConnectionString = _ "Provider=Microsoft.Jet.OLEDB.4.0;" _ "Data Source= |DataDirectory|\Employees.mdb"

34 Storing Connection Strings A connection string is stored in two files –The App.config file stores the connection string –It's also added to the resource file named Settings.settings

35 Figure 11-18: Connection String Appearing in the Application's Resource File

36 The OleDbConnection Class A connection is represented by the System.Data.OleDb.OleDbConnec tion class –Properties The ConnectionString property stores the connection string The Open method opens the connection The Close method closes an open connection

37 Introduction to the TableAdapter and OleDbDataAdapter Classes The TableAdapter class is used to send requests over a connection The TableAdpater performs the following tasks: –It opens a connection using an instance of the OleDbConnection class –It sends an SQL SELECT statement to retrieve data The provider returns the data The data is stored in a DataSet and DataTable The TableAdapter class is new to Visual Studio 2005

38 Introduction to the OleDbDataAdapter Class The TableAdapter uses an underlying OleDbDataAdatpter to retrieve and update database data SQL statements are stored in an instance of the OleDbCommand class –The SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand properties store these references The data type of these properties is OleDbCommand

39 The OleDbCommand Class The OleDbCommand class stores SQL statements The Connection property stores a reference to an OleDbConnection The CommandText property stores an SQL statement The CommandType should be set to Text

40 Configuring an OleDbCommand Object (Example) Me.m_commandCollection(0).Connection = Me.Connection Me.m_commandCollection(0).CommandText = "SELECT " & _ "fldEmployeeID, fldFirstName, fldLastName, " & _ "fldTelephone, fldDateHired, fldWage, " & _ "fldDeductions, fldNotes, fldType FROM tblEmployees" Me.m_commandCollection(0).CommandType = _ System.Data.CommandType.Text Me.Adapter.SelectCommand = Me.CommandCollection(0)

41 Populating and Updating a TableAdapter Calling the Fill method populates a DataSet and DataTable Calling the Update method saves changes made to the DataSet back to the database

42 Figure 11-19: Using the TableAdapter to Select and Update Database Data

43 Filling A DataSet (Example) The Wizard adds the following statement to the form's Load event handler to populate a DataSet : Private Sub frmMain_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Me.TblEmployeesTableAdapter.Fill( _ Me.EmployeesDataSet.tblEmployees) End Sub

44 Updating a DataSet (Example) The Update method is called on the TableAdapter to update a DataSet as follows: Me.TblEmployeesTableAdapter.Update( _ Me.EmployeesDataSet.tblEmployees)

45 Introduction to the BindingSource Class Navigation from record to record is accomplished using the BindingSource class The BindingSource class is new to Visual Studio 2005 Two properties are used to bind data –The DataSource property is set to a DataSet –The DataMember stores a string containing a table in the DataSet

46 The BindingSource Class (Members) The Count property stores the number of records contained in the data source The Current property gets the current record The Position property gets the index of the current list item The methods named MoveFirst, MoveNext, MovePrevious, and MoveLast perform navigation

47 Introduction to the BindingNavigator Class The BindingNavigator works in conjunction with the BindingSource –It supplies the means for the end user to navigate from record to record –The BindingNavigator is made up of a toolbar with buttons –The BindingNavigator is new to Visual Studio 2005

48 Table 11-1: Relationship Between BindingNavigator and BindingSource members

49 Binding Control Instances with the Binding Class The Binding class is used to bind control instances to a data source Controls support a collection named DataBindings The collection stores instances of the Binding class

50 The Binding Constructor The first argument contains the property to bind –Text for a TextBox for example The second argument contains the binding source The third argument contains a table in the binding source The final argument controls data formatting

51 The Binding Constructor (Example) Bind the Text property Me.FldEmployeeIDTextBox.DataBindings.Add ( _ New System.Windows.Forms.Binding("Text", _ Me.TblEmployeesBindingSource, _ "fldEmployeeID", True))

52 Introduction to the Untyped DataSet and DataTable Classes The System.Data.DataSet class stores an in-memory representation of one or more database tables –Each table is represented as a DataTable object –The Tables property of the DataSet stores a reference to a collection of DataTable objects One DataTable object exists for each table in the DataSet –Reference a DataTable using a 0-based index value or a string key

53 Referencing a DataTable (Example) Reference the first DataTable in the DataSet named EmployeesDataSet using a numeric index and a string key Dim CurrentTable As _ System.Data.DataTable CurrentTable = _ EmployeesDataSet.Tables(0) CurrentTable = _ EmployeesDataSet.Tables("tblEmployees")

54 Introduction to the Untyped DataTable Class The DataTable class supports properties to get information about the current table –The TableName property gets the name of the table –The Rows property gets a collection of rows ( DataRow objects)

55 Introduction To the Untyped DataRow Class The Rows property of the DataTable class references a collection of rows The Count property of the Rows collection returns the number of rows Each item in the collection has a data type of DataRow –Each item in the collection represents a row in the table

56 The DataRow Class (Examples) Get the number of rows in the DataTable named CurrentTable Dim RowCount As Integer RowCount = CurrentTable.Rows.Count Get the first row from the table named tblEmployees Dim CurrentRow As System.Data.DataRow CurrentRow = _ EmployeesDataSet.Tables("tblEmployees"). _ Rows(0)

57 Referencing a Field in a DataRow Use the Item member of the DataRow class to reference a field –The Item member accepts an Integer index or string key containing the field name Example to reference the field named fldEmployeeID: Dim CurrentID As String CurrentID = _ CurrentRow.Item("fldEmployeeID").ToString

58 Introduction to the DataColumn Class A database table contains one or more columns The Columns collection of the DataTable class stores a reference to the columns The DataColumn class of the Columns collection stores a reference to an individual column –The ColumnName property stores the name of the column –The Caption property stores a descriptive caption –The MaxLength property stores the maximum length (number of characters) that can be stored in the column This value is inferred for numeric data types

59 The DataColumn Class (Example) Examine the first column in the DataTable named CurrentTable Dim CurrentTable As DataTable Dim CurrentColumn As DataColumn CurrentTable = EmployeesDataSet.tblEmployees CurrentColumn = CurrentTable.Columns(0) txtColumnName.Text = _ CurrentColumn.ColumnName txtUntypedOutput.Text = _ CurrentColumn.DataType.ToString

60 Introduction to Strongly Typed DataSets Strongly typed DataSets are generated by a Wizard The Wizard creates properties and methods corresponding to the underlying tables in the database –Strongly typed DataSets eliminate type conversion errors –Strongly typed DataSets support Intellisense technology

61 Figure 11-22:DataSet Files Appearing in the Solution Explorer

62 Implementation of a Strongly Typed DataSet A strongly typed DataSet is just a class that inherits from the base System.Data.DataSet class Example: Partial Public Class EmployeesDataSet Inherits System.Data.DataSet End Class Its members correspond to the underlying tables and fields in those tables

63 Implementation of a Strongly Typed DataTable The strongly typed DataSet class contains a strongly typed DataTable class It inherits from the base System.Data.DataTable class Example: Partial Public Class tblEmployeesDataTable Inherits System.Data.DataTable End Class

64 Strongly Typed DataRows A strongly typed DataRow class is just a class that inherits from the base System.Data.DataRow class Example: Partial Public Class tblEmployeesRow Inherits System.Data.DataRow End Class

65 Using a Strongly Typed DataRow Using a strongly typed DataRow, it's possible to reference the fields directly Example: Dim CurrentID As String CurrentID = CurrentRowTyped.fldEmployeeID.ToString


Download ppt "Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load."

Similar presentations


Ads by Google