VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.

Slides:



Advertisements
Similar presentations
Coding ADO.Net DataSet Objects ISYS 512. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set.
Advertisements

Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
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.
Chapter 10 ADO. What is ADO? ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a programming interface to access data in a database.
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
Introduction to Database Processing with ADO.NET.
Chapter 10 Accessing Database Files Programming In Visual Basic.NET.
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.
Coding ADO.NET Objects: Connection, Command, DataReader.
Using ADO.NET Chapter Microsoft Visual Basic.NET: Reloaded 1.
Introduction to Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
Coding ADO.NET Objects: Connection, Command, DataReader.
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.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
Visual Basic Database Access BICS546. Microsoft Universal Data Access OLE DB: The OLE database protocol –Allows a program to access information in any.
VB.NET Database Tools ISYS 573. 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. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Coding ADO.NET Objects: Connection, Command, DataReader.
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.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
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.
Introduction to ADO.Net and Visual Studio Database Tools
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Some Basic Database Terminology
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
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.
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.
CHAPTER EIGHT Accessing Data Processing Databases.
CHAPTER EIGHT Accessing Data Processing Databases.
Objectives In this lesson, you will learn to: *Identify the need for ADO.NET *Identify the features of ADO.NET *Identify the components of the ADO.NET.
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.
ADO.NET Objects – Data Providers Dr. Ron Eaglin. Requirements Visual Studio 2005 Microsoft SQL Server 2000 or 2005 –Adventure Works Database Installed.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
 It is the primary data access model for.Net applications  Next version of ADO  Can be divided into two parts ◦ Providers ◦ DataSets  Resides in System.Data.
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.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
Databases and ADO.NET Programming Right from the Start with Visual Basic.NET 1/e 11.
Module 3: Working with Local Data. Overview Using DataSets Using XML Using SQL Server CE.
HNDIT Rapid Application Development
Coding ADO.NET Objects: Connection, Command, DataReader.
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.
Module 2: Using ADO.NET to Access Data. Overview ADO.NET Architecture Creating an Application That Uses ADO.NET to Access Data Changing Database Records.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350.
Coding ADO.NET Objects: Connection, Command, DataReader.
Visual Basic Database Access
Introduction to Database Processing with ADO.NET
Coding ADO.NET Objects: Connection, Command, DataReader
Introduction to Database Processing with ADO.NET
ADO.NET Framework.
VB.NET Using Database.
Chapter 10 ADO.
Database Applications
Coding ADO.NET Objects: Connection, Command, DataReader
Chapter 10 Accessing Database Files
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

VB.NET Database Access ISYS546

Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE DB: The OLE database protocol –Allows a program to access information in any type of data source. –Data provider: databases, spreadsheets, etc. ADO.NET: ActiveX Data Objects –An Interface for OLE DB. –Allow programmers to use a standard set of objects to refer to any OLE DB data source.

Program ADO.NET OLE DB Provider Data Source OLE DB Provider ODBC Data Source

Using ODBC Windows 2000: Control Panel /Administrative Tools/DataSource(ODBC) Three types of data source names –User DSN: usable only by you and only on the machine currently using. –System DSN: Any one using the machine can use. –File DSN: Can be copied and used by other computers with the same driver installed.

VB.NET Database Tools Database connection: –Tool/Connect to database Provider:MS Jet 4.0 OLE DB Provider Connection Server Explorer –Data connections: Right click and Add Connection Tables, Views –Server Right click server to access New Database designer. Database diagrams (set relationships) Tables (right click to access the context menu for Table Designer) Views (right click to access the context menu for View Designer) Stored procedures Toolbox Data tab Data Form Wizard

Steps to Retrieve Data Establishes a connection to the database. Executes commands against the database. Store data results.

ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database

Connection Object Represent a connection to the database. Two ways to create a connection: –Visual tool provided with Visual Studio.NET –Using code: –dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" –dim objConn as new OledbConnection(strConn) –objConn.open()

Command Object The command object allows us to execute a SQL statement or a stored procedure. Properties: –CommandType, CommandText, Connection Basic Methods: –ExecuteReader: Creates a DataReader object that contains the results of the query. –ExecuteNonQuery: Execute SQL’s INSERT, DELETE, UPDATE statements.

DataReader Object It is a read-only and forward-only pointer into a table to retrieve records. Basic Methods: –Read: Reads the current record and advances the pointer to the next record. –Close: Closes the dataReader.

DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted data. Data is extracted from the database and stored in the DataSet object. Updates to the DataSet must copy back to the database to make the changes permanent.

DataSet and Related Objects DataAdapter: This the object used to pass data between the database and the dataset. The Fill method copies the data into the dataset, and the Update method copies the updates back into the database. DataSet: Can contain multiple tables and relationships. DataTable object: Represents a table in the dataset. DataView: This represents a specific view of the DataTables held in the dataset.

DataSet and Related Objects DataView DataSet DataTable DataAdapter Connection Command

Data Form Wizard Creating a form with data-bound controls to display and update information in a dataset. Demo: Using Data Form Wizard to create a navigational form. –Project/Add Windows Form/Data Form Wizard –Set connection –Choose tables –Display records in grid or in text boxes.

Adapter & Dataset Context Menu Adapter: –Configure Adapter –Generate dataset –Preview data Dataset: –View Schema: Dataset/XML

Other Data Form Demos Connecting to a SQL Server database. Display records in text boxes. Add /Modify/Delete records. Hierarchical forms.

Creating A Database Application Without Programming Creating a database application to display information and update database. A main form with buttons to open data forms: –DisplayInfo –Enter New –Modify –Exit

Using Tool Box’s Data Tab Creating a Connection, setting up a Data Adapter, and generating a dataset: –Drag OledbDataAdapter (or database’s table) to the form. –Use the Data Adapter Wizard to configure the Adapter. –Right Click the Adapter to preview data and creating dataset. Bind the dataset to controls.

Data Binding Connect a control or property to one or more data elements. Simple binding: Use simple binding to display a field value in controls that show Data Bindings in the property window, such as text box or label. Complex binding: Use complex binding to bind more than one field to controls such as DataGrid and list box. Use the control’s Data Source and Data Member to bind the data.

Creating Bound Controls DataGrid control: –Data Source property –Data Member property –In the Form Load event, use Adapter’s Fill method to load the dataset: OleDbDataAdapter1.Fill(DataSet11)

Binding Text Box Data Bindings property: –Text: choose field Add navigation buttons: –The current record position within the dataset is stored in a form’s BindingContext’s Position property. This position is zero based. Add one move to the next record, minus one move to the previous record.

MoveNext and MoveLast Example MoveNext: – Me.BindingContext(DataSet21, "customer").Position += 1 MoveLast: –Me.BindingContext(DataSet21, "customer").Position = Me.BindingContext(DataSet21, "customer").Count -1 How to MovePrevious and MoveFirst?

Binding DataGrid From Server Explorer, drag the table from a database connection (or from Data tab, drag a oleDbAdapter) onto the form. Create dataset. Drag DataGrid and set the DataSource and Data Member property. Use adapter’s Fill method to load the dataset.

Displaying Many Tables with One DataGrid Define one Adapter for each table. Create the dataset with multiple tables. Add a DataGrid control and set the DataSource proeprty to the dataset name and leave the DataMember property blank.

Creating Hierarchical Data Grid Define two Adapters, one for the parent table and one for the child table. Create the dataset. Right-click the dataset to View Schema Right-click the parent table and choose Add/New Relation Add a DataGrid control and set the DataSource proeprty to the dataset.parentTable and leave the DataMember property blank.

Binding ListBox Example: Bind Customer Table’s CID field to a listbox. –Create a Adapter to retrieve CID (and Cname) fields, and generate the dataset. –Add ListBox and set binding properties: Data Source Data Member Value Member: the actual values for items in the list box. To display the selected item’s value in a text box, do: –Textbox1.text = ListBox1.SelectedValue Can we use TextBox1.text=ListBox1.SelectedItem? No!

ListBox SelectedItem Property After binding to a data source, this property return a DataRowView object. What is DataRowView? –Object Browser: System.Data –System.Data »DataRowView: Item property To retrieve a column from a DataRowView object (use 0-based index to identity a column): ListBox1.SelectedItem.Item(1) Or: ListBox1.SelectedItem(1) Or: ListBox1.SelectedItem(“Cname”)

Using Object Browser View/OtherWindows/Object Browser DataSet object model: –DataSet Relations Tables –Rows –Columns Use Object Browser to study object’s properties, methods.

Display Records in Unbound Text Boxes Use code to assign field value to the text box’s text property. Example: –Dim drFound As DataRow –drFound = DataSet11.CUSTOMER.Rows(0) Or DataSet11.Tables(“CUSTOMER”).Rows(0) –TextBox4.Text = drFound.Item("cname") Or drFound.Item(1) –Or: TextBox4.Text = DataSet11.CUSTOMER.Rows(0).Item(1) –Or: DataSet21.Tables.Item("customer").Rows.Item(0).Item(1)

Implement MoveNext Button with Unbound Control rowIndex += 1 If rowIndex < DataSet11.CUSTOMER.Rows.Count Then TextBox1.Text = DataSet11.Tables("customer").Rows(rowIndex).Item(0) TextBox2.Text = DataSet11.CUSTOMER.Rows(rowIndex).Item(1) Else MsgBox("out of bound") End If Note: MovePrevious, MoveLast, MoveFirst?

Using Object Browser to Study OleDB Object System.Data –System.Data.OleDB OleDBConnection –Methods: New(), New(ConnectionString), Open(), Close() –Properties: ConnectionString, DataBase, Provider, TimeOut OleDBCommannd –Methods: ExecuteReader, ExecuteNonQuery –Properties: Connection, CommandType, CommandText, Parameters OleDBDataAdapter –Methods: Fill –Properties: SelectCommand, InsertCommand, DeleteCommand, UpdateCommand.

Another Way to Bind Listbox and Display Selected Record Create an adapter to retrieve Customer records and create a dataset. Bind the CID field to the listbox. Use the Find method of Table’s Rows collection to find the record. Display the found record in unbound text boxes.

Code Example Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Dim drFound As DataRow drFound = DataSet41.CUSTOMER.Rows.Find(ListBox1.SelectedValue) TextBox1.Text = drFound.Item("cname") TextBox2.Text = drFound.Item("rating") End Sub

Creating Parameter Query with Adapter Configuration Wizard Parameter query: Selection criteria is entered at run time. Example: Orders table: OID, CID, Odate, SalesPerson To create a parameter for the CID field: –In the Query Design window’s criteria column of the CID field, add criteria: =? To assign the parameter value: –OleDbDataAdapter2.SelectCommand.Parameters("cid").Value = Demo: Get CID from a InputBox and display orders.

Parameter Query Example: Select CID from a listBox and display orders of the selected CID in a DataGrid –Create and bind the listbox. –Create a second adapter and define a parameter query. –In the Query Design window’s criteria column, add criteria: =? –Generate the dataset (DataSet11 in this example) with the parameter. –Create and bind the DataGrid to the dataset. –In the listbox’s click event, assign the selected value to the parameter and fill the dataset: DataSet11.Clear() OleDbDataAdapter2.SelectCommand.Parameters("cid").Value = ListBox1.SelectedValue OleDbDataAdapter2.Fill(DataSet11)

Display Selected Record in Text Boxes with Parameter Query Create and bind the listbox. Create a second adapter and define a parameter query. –In the Query Design window’s criteria column, add criteria: =? Generate the dataset with the parameter. Create and bind textboxes to the dataset. In the listbox’s click event, assign the selected value to the parameter and fill the dataset: –DataSet11.Clear() –OleDbDataAdapter2.SelectCommand.Parameters("cid").Value = ListBox1.SelectedValue –OleDbDataAdapter2.Fill(DataSet11)

Send Changes in DataGrid Back to the Database Updating records in DataGrid: –New records are added at the end of the grid. –To delete a record, click the leftmost column to select the record, then press the delete key. –Modify record Add an Update button that use adapter’s update method to send changes back to the data source: –OledbDataAdapter1.Update(Dataset11)