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

Slides:



Advertisements
Similar presentations
Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
Advertisements

Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
 Open the Paradise.exe file  Application displays records in the Books database  Allows the store manager to enter an author’s name (or part of a name)
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
1 Web-Enabled Decision Support Systems Advance Topics in Database Connectivity Prof. Name Position (123) University Name.
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
Introduction to Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
1 Pertemuan 09 Database Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Microsoft Visual Basic 2012 CHAPTER TEN Incorporating Databases with ADO.NET.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS546.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Access Tutorial 10 Automating Tasks with Macros
From VS C# 2010 Programming, John Allwork 1 VS2010 C# Programming - DB intro 1 Topics – Database Relational - linked tables SQL ADO.NET objects Referencing.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Databases and LINQ Visual Basic 2010 How to Program 1.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Thirteen Working with Access Databases and LINQ.
Connecting to Data Sources Using ADO.NET Dr. Awad Khalil Computer Science & Engineering Department AUC.
CHAPTER EIGHT Accessing Data Processing Databases.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET.
Using Adapter Wizard ISYS 512. Data Adapter Wizard – 2 nd Level of Using ADO.Net Configure Data Adapter and generating a dataset: –From the Data tab of.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Mauricio Featherman, Ph.D. Washington St. University
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 25 I’m Suffering from Information Overload.
1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases.
Chapter 24 I’m Suffering from Information Overload (Access Databases) Clearly Visual Basic: Programming with Visual Basic nd Edition.
HNDIT Rapid Application Development
ADO.NET in VB.NET 2005 ITE 370 4/26/2017.
1 Database Programming with ADO.NET Kashef Mughal.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Understand Databound Controls Windows Development Fundamentals LESSON 4.2A.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.
Creating SQL Database file And Displaying a Database Table in a DataGridView.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
Database, SQL and ADO.NET
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Visual Basic 2010 How to Program
ADO.NET Framework.
Incorporating Databases with ADO.NET
Incorporating Databases with ADO.NET
VB.NET Using Database.
CIS16 Application Programming with Visual Basic
CIS16 Application Programming with Visual Basic
Database Applications
Chapter 10 Accessing Database Files
Presentation transcript:

Chapter 11 Introduction to Database Processing

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

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

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

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

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

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

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

Figure 11-2: Connecting to a Database

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

Figure 11-3: Data Sources Window

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

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

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

Figure 11-5: Control Instances Bound to a DataSet

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

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

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

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

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

Figure 11-9: Add Connection Dialog Box

Figure 11-10: Change Data Source Dialog Box

Figure 11-11: Test Connection Message Box

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

Figure 11-13: Local Database File Message Box

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

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

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

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

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

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

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

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"

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

Table 11-1: Relationship Between BindingNavigator and BindingSource members

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

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

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

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

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")

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)

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

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)

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

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

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

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

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

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

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

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

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