Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE."— Presentation transcript:

1 VB.NET Database Tools ISYS 573

2 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: –An Interface for OLE DB. –Allow programmers to use a standard set of objects to refer to any OLE DB data source.

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

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

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

6 Connection Object Represent a connection to the database.

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

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

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

10 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 Toolbox Data tab Data Form Wizard

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

12 Adapter & Dataset Context Menu Adapter: –Configure Adapter –Generate dataset –Preview data –Properties: Select/Insert/Update/DeleteCommand Dataset: –View Schema: Dataset/XML

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

14 Data Binding Connect a control or property to one or more data elements.

15 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. –OleDbDataAdapter1.Fill(DataSet11

16 Introduction to ADO.Net Programming

17 Import NameSpace The Imports statement must appear before all other declarations in a file and cannot appear inside a class or module declaration. –Imports System.Data.OleDb –Public Class Form1

18 Connection Object Example: –dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" –dim objConn as new OledbConnection(strConn) –objConn.open() Basic Methods: –Open, Close

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

20 ExecuteReader Example dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" dim objConn as new OledbConnection(strConn) dim strSQL as string = "select * from customer;" dim objComm as new OledbCommand(strSQL,objConn) dim Results as string objConn.open() dim objDataReader as oledbDataReader objDataReader=objComm.executeReader()

21 DataReader Object It is read-only and forward-only cursor. Basic Methods: –Read: Reads the current record and advances the pointer to the next record. –Close: Closes the dataReader.

22 Read Records in a DataReader dim Results as string do while objDataReader.Read()=true Results+=objDataReader("cid") + “ “ + objDataReader("Cname") + vbCrLF loop Textbox1.text=Results Note: objDataReader.Item(0) Note: objDataReader.Item(“cid”)

23 Add Items from a DataReader to a Listbox Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select cid from customer;" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() Do While objDataReader.Read() = True LISTBOX1.Items.Add(objDataReader("cid")) Loop

24 Display Selected Customer’s Record Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Dim strSQL As String = "select * from customer where cid = '" & ListBox1.SelectedItem & "'" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() If objDataReader.Read = True Then TextBox1.Text = objDataReader("Cname") TextBox2.Text = objDataReader("City") TextBox3.Text = objDataReader("rating") Else MessageBox.Show("record not found") End If objConn.Close() End Sub

25 Insert a New Record Using ExecuteNonQuery Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQLInsert As String strSQLInsert = "Insert into Customer values ('" strSQLInsert = strSQLInsert & TextBox1.Text & "','" & TextBox2.Text & "','" strSQLInsert = strSQLInsert & TextBox3.Text & "','" & TextBox4.Text & "')" Dim objComm As New OleDbCommand(strSQLInsert, objConn) objConn.Open() objComm.ExecuteNonQuery() objConn.Close() End Sub

26 Demo Create a project that do the following tasks: –Use a DataReader to retrieve customer IDs and populate a listbox. –Select a new rating from radio buttons for the selected customer. –Update customer’s rating using the ExecuteNonQuery method of a Command object.

27 Declare OleDB objects and create listbox Imports System.Data.OleDb Public Class Form3 Inherits System.Windows.Forms.Form Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select cid from customer;" Dim objComm As New OleDbCommand(strSQL, objConn) Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() Do While objDataReader.Read() = True ListBox1.Items.Add(objDataReader("cid")) Loop objConn.Close() End Sub

28 Update customer rating Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click objConn.Open() Dim newRating As String If RadioButton1.Checked = True Then newRating = "A" ElseIf RadioButton2.Checked Then newRating = "B" Else newRating = "C" End If Dim strSQLUpd As String = "Update customer set rating = '" & newRating & "'" strSQLUpd = strSQLUpd & " where cid='" & ListBox1.SelectedItem & "'" Dim objCommUpd As New OleDbCommand(strSQLUpd, objConn) objCommUpd.ExecuteNonQuery() objConn.Close() End Sub


Download ppt "VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE."

Similar presentations


Ads by Google