VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity...

29
VB .NET Database Access ISYS 812
  • date post

    15-Jan-2016
  • Category

    Documents

  • view

    223
  • download

    0

Transcript of VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity...

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

VB .NET Database Access

ISYS 812

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

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.

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

Program

ADO.NET

OLE DBProvider

Data Source

OLE DBProvider

ODBC

ODBCData Source

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

Steps to Retrieve Data

• Establishes a connection to the database.

• Executes commands against the database.

• Store data results.

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

ADO.NET Objects

Data Set

.NET Applications

Data Reader

Command Object

Connection Object

Managed Data Provider(OLEDB)

Database

Adapter

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

ADO.NET Objects

• Connection Object: Represent a connection to the database.

• Command Object: The command object allows us to execute a SQL statement or a stored procedure.

• DataSet Object: A DataSet object can hold several tables and relationships between tables.

• DataAdapter: This the object used to pass data between the database and the dataset.

• DataReader: It is a read-only and forward-only pointer into a table to retrieve records.

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

How to create an ADO.Net object?

• Using Data Wizard

• Using code: – Example:– dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\

sales2k.mdb"

– dim objConn as new OledbConnection(strConn)

– objConn.open()

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

Connecting to Database

• Tool/Connect to database• Provider:MS Jet 4.0 OLE DB Provider

• Connection

• Server Explorer– Data connections:

• Right click and Add Connection

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

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.

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

Other Data Form Demos

• Add /Modify/Delete records.

• Hierarchical forms.

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

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

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

Using Adapter Wizard

• 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.

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

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.

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

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)

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

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.

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

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?• Note: The Position property takes care of the end of file

automatically.

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

CurrencyManager

• Dim custCurrMgr As CurrencyManager

• Dim ordCurrMgr As CurrencyManager

• In a procedure:– ordCurrMgr = Me.BindingContext(Ds31, "orders")

– custCurrMgr = Me.BindingContext(Ds31, “customer")

– custCurrMgr.Position += 1

– ordCurrMgr.Position += 1

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

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.

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

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.

• Note: DO File/SaveAll after creating the relation.

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

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!

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

ListBox SelectedItem Property

• How to display the selected record?• 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”)

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

Using Object Browser

• View/OtherWindows/Object Browser• DataSet object model:• System.Data

– DataSet• Relations• Tables

– Rows– Columns

• Use Object Browser to study object’s properties, methods.

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

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)

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

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?

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

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.

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

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.

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

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)

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

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)

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

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)