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

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

Introduction to Database Processing with ADO.NET.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Authenticating Users in an ASP.NET Application. Web Site Administration Tool From VS 2008, click Website/ ASP.Net Configuration to open Web Site Administration.
Introduction to ADO.NET Programming. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB)
ASP.NET and ADO.NET. ASP.NET Server Controls Intrinsic Controls: These controls correspond to their HTML counterparts. –Ex. Textbox, listbox, button,
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.
Introduction to Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm.
ASP.NET and ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
1 ADO.NET. 2.NET Framework Data Namespaces System.Data –Base set of classes and interfaces for ADO.NET System.Data.Common –Classes shared by the.NET Data.
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.
Web Site Security ISYS 512/812. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows:
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Working with Session and Application Objects. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and.
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
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.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to Web Application Development with.Net and Web Service ISYS 350.
ASP.NET and ADO.NET. Bind the DataReader to a DataGrid Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn.
Updating Database. Two Approaches to Update Database 1. Using the DataSet object, the updates are first applied to the DataSet then applied to the database.
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
VB.NET Classes ISYS 812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Working with Session. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values.
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.
VB.NET Classes ISYS 546. Classes A class is program structure that defines a user-defined data type that are used to create objects. An instance of a.
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 Web Service ISYS 512. Web Service XML Web Service Web services are classes that are stored on the web which can instantiate and use in.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
ASP and HTML. Anchor Tag testAnchorTag Demo: ASPNet/ASPNETProdListSelf.ASPX.
Introduction to Structured Query Language SQL. SQL Select Command SELECT * FROM tableName WHERE criteria;
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.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
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.
CIS 451: Using ASP.NET Objects with SQL Dr. Ralph D. Westfall February, 2009.
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.
Databases and Data Access  Introduction to ADO.NET  ADO.NET objects  ADP.NET namespaces  Differences between ADO and ADO.NET.
1 Introduction to ADO.NET Microsoft ADO.NET 2.0 Step by Step Rebecca M Riordan Microsoft Press, 2006.
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.
ASP.Net and HTML. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data.
ADO.NET Objects – Data Providers Dr. Ron Eaglin. Requirements Visual Studio 2005 Microsoft SQL Server 2000 or 2005 –Adventure Works Database Installed.
Command Object’s ExecuteNonQuery Method ISYS 512.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
HNDIT Rapid Application Development
Coding ADO.NET Objects: Connection, Command, DataReader.
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.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
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.
Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out.
Coding ADO.NET Objects: Connection, Command, DataReader.
Coding ADO.NET Objects: Connection, Command, DataReader
ADO.NET Framework.
ASP.NET and ADO.NET.
VB.NET Using Database.
Coding ADO.NET Objects: Connection, Command, DataReader
Database Handling Class and Service
ASP.NET and ADO.NET.
VB Classes ISYS 512.
Working with Session and Application Objects
Introduction to ADO.NET
Command Object’s ExecuteNonQuery Method
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

Introduction to ADO.NET

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

Connection with a Connection Object A connection object represents a unique session with a data source. Connection string: database, OLE DB provider, password, if any, security, etc. Use the Open/Close method to open/close a connection. Manage transaction: BeginTransaction(), Commit, rollBack.

Connection String Containing information about database, OLE DB provider, password, if any, security, etc. For Jet database: –Provider=Microsoft.Jet.OLEDB.4.0;Persist Security info = False;Data Source=c:\ …\Nwind.mdb

Data Link Properties Access permissions –Read—Read only. –ReadWrite—Read and write. –Share Deny None—Neither read nor write access can be denied to others. –Share Deny Read—Prevents others from opening in read mode. –Share Deny Write—Prevents others from opening in write mode. –Share Exclusive—Prevents others from opening in read/write mode. –Write—Write only.

Providers MSDASQL –Microsoft OLE DB Provider for ODBC Microsoft.Jet.OLEDB.3.51 Microsoft.Jet.OLEDB.4.0 MSDAORA – For Oracle SQLOLEDB – Microsoft SQL Server

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 –BeginTransaction

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.

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

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.

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

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 cname 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("cname")) Loop

ExecuteNonQuery Example dim strSQLUpd as string="Update customer set rating = ‘A’” strSQLUpd=strSQLUpd & " where cname='" & CnameList.selectedItem.text & "'“ Dim objCommUpd As New OleDbCommand(strSQLUpd, objConn) objCommUpd.ExecuteNonQuery()

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.

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

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

Use Command Object’s ExecuteNonQuery to Insert A New Record 1.Create unbound text boxes to enter new record. 2.Add an Insert button with the following handler Dim strSQLInsert As String strSQLInsert = "Insert into Customer values ('" strSQLInsert = strSQLInsert & TextBox1.Text & "','" & TextBox2.Text & "','" strSQLInsert = strSQLInsert & TextBox3.Text & "','" & TextBox4.Text & "')" Dim objCommInsert As New OleDbCommand(strSQLInsert, objConn) objCommInsert.ExecuteNonQuery()

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 DataSet: Can contain multiple tables and relationships. DataTable object: Represents a table in the dataset. 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. DataView: This represents a specific view of the DataTables held in the dataset.

DataSet and Related Objects DataView DataSet DataTable DataAdapter Connection Command

Reading Data into a Table 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 objDataSet as new Dataset() dim objAdapter as new OledbDataAdapter(strSQL, objConn) objAdapter.Fill(objDataSet, "Cust")

Binding a Table in a Dataset to a DataGrid Imports System.Data.OleDb Public Class Form4 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 objDataSet As New DataSet() Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strSQL As String = "select * from customer;" Dim objAdapter As New OleDbDataAdapter(strSQL, objConn) objAdapter.Fill(objDataSet, "Cust") DataGrid1.DataSource = objDataSet DataGrid1.DataMember = "Cust" End Sub

DataView Object The DataView object exposes a complete table or a subset of the records from a table. –Dim ObjDataView As New DataView() –ObjDataView.Table = objDataSet.Tables("Cust") Properties: –RowFilter –objDataView.rowfilter="rating='" & Ratinglist.SelectedItem.Text & "'“ DataView can be used as a DataSource in data binding.

DataView Example Dim ObjDataView As New DataView() Dim newRating As String If RadioButton1.Checked = True Then newRating = "A" ElseIf RadioButton2.Checked Then newRating = "B" Else newRating = "C" End If ObjDataView.Table = objDataSet.Tables("Cust") ObjDataView.RowFilter = "rating='" & newRating & "'" DataGrid1.Visible = True DataGrid1.DataSource = ObjDataView

Defining an Adapter without A Command Object 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 objDataSet as new Dataset() dim objAdapter as new OledbDataAdapter(strSQL, objConn) objAdapter.Fill(objDataSet, "Cust")

Defining an Adapter with A Command Object dim strSQLCust as string = "select * from customer;" dim objComm as new OledbCommand() dim objAdapter as new OledbDataAdapter() dim objDataSet as new Dataset() objComm.Connection=objConn objComm.CommandType=COmmandType.Text objComm.CommandText=strSQLCust objAdapter.SelectCommand=objComm objConn.open() objAdapter.Fill(objDataSet, "Customer") Note:Adapter has SelectCommand, InsertCommand, DeleteCommand, UpdateCommand properties (Check Object Browser).

Creating Multiple Tables in A DataSet dim strSQLCust as string = "select * from customer;" dim strSQLOrder as string ="select * from orders;" dim objComm as new OledbCommand() dim objAdapter as new OledbDataAdapter() dim objDataSet as new Dataset() objComm.Connection=objConn objComm.CommandType=COmmandType.Text objComm.CommandText=strSQLCust objAdapter.SelectCommand=objComm objConn.open() objAdapter.Fill(objDataSet, "Customer") objComm.COmmandText=strSQLOrder objAdapter.Fill(objDataSet, "Orders")

Adding Relationship to a Dataset The Dataset object has a Relations property. It is a collection of DataRelations. We can use a relationship to enforce the referential integrity. To define a DataRelation: –DataRelObj=DataRelation(RelationName, ParentTable Field, ChildTableField) objRel = New DataRelation("custOrder", objDataset.tables("customer").columns("cid"), objDataset.tables("orders").columns("cid"))

DataTable’s Rows Property This is a collection of all the records in a table, a collection of DataRow objects. The DataRow object has a GetChildRows method that returns a collection of rows from another table that are related as child rows to this row.

Access Rows in a DataRow Collection dim objTable as DataTable = objDataset.Tables("Customer") dim objRow as DataRow For each objRow in objTable.Rows strResult=strResult+“ " & objRow("cid") & " " & objRow("cname") & vbCrLf Next

Displaying Parent/Child Records in a Relation Define the relation. Specify the relation in the GetChildRows method of the DataRow object.

Imports System.Data.OleDb Public Class Form1 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 strSQLCust As String = "select * from customer;" Dim strSQLOrder As String = "select * from orders;" Dim objComm As New OledbCommand() Dim objAdapter As New OledbDataAdapter() Dim objDataSet As New DataSet() Dim objRel As DataRelation Dim strResult As String Dim rowIndex As Integer = 0 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load objComm.Connection = objConn objComm.CommandType = CommandType.Text objComm.CommandText = strSQLCust objAdapter.SelectCommand = objComm objConn.Open() objAdapter.Fill(objDataSet, "Customer") objComm.CommandText = strSQLOrder objAdapter.Fill(objDataSet, "Orders") objRel = New DataRelation("custOrder", objDataSet.Tables("Customer").Columns("cid"), objDataSet.Tables("Orders").Columns("cid")) objDataSet.Relations.Add(objRel) Call showChild(rowIndex) End Sub

Private Sub showChild(ByVal RowIndex) TextBox1.Text = objDataSet.Tables("customer").Rows(RowIndex).Item("cid") TextBox1.Text = objDataSet.Tables("customer").Rows(RowIndex).Item("cname") Dim objOrderRel As DataRelation = objDataSet.Tables("customer").ChildRelations("custOrder") Dim strResult As String Dim objChildRow As DataRow For Each objChildRow In objDataSet.Tables("customer").Rows(RowIndex).GetChildRows(objOrderRel) strResult = strResult + objChildRow("oid") + " " + objChildRow("odate") + vbCrLf Next TextBox3.Text = strResult End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click rowIndex += 1 Call showChild(rowIndex) End Sub

Display Child Records in Grid This technique uses the fact that the DataView of a table can be used as a DataSource in binding a data grid. Assuming a Customer/Orders relationship, for each customer record: –Use Table object’s Clone method to create a temporary child table structure. –Use Table object’s ImportRow method to import child records to the clone table. –Use the clone table’s Dataview to bind the DataGrid.

Dim objRow As DataRow Dim drFound As DataRow drFound = objDataSet.Tables("Customer").Rows("0") TextBox1.Text = drFound.Item("cid") TextBox2.Text = drFound.Item("cname") Dim childTable As DataTable childTable = objDataSet.Tables("orders").Clone For Each objRow In drFound.GetChildRows(objOrderRel) childTable.ImportRow(objRow) Next DataGrid1.DataSource = childTable.DefaultView

Multiple Forms: Where to import the oledb, and where to create the oledb objects Imports System.Data.OleDb Module Module1 Public strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Public objConn As New OleDbConnection(strConn) Public strSQLCust As String = "select * from customer;" Public strSQLOrder As String = "select * from orders;" Public objComm As New OleDbCommand() Public objAdapter As New OleDbDataAdapter() Public objDataSet As New DataSet() Public objRel As DataRelation Public strResult As String Public rowIndex As Integer = 0 Public ObjDataView As New DataView() Public Sub main() objComm.Connection = objConn objComm.CommandType = CommandType.Text objComm.CommandText = strSQLCust objAdapter.SelectCommand = objComm objConn.Open() objAdapter.Fill(objDataSet, "Customer") objComm.CommandText = strSQLOrder objAdapter.Fill(objDataSet, "Orders") End Sub End Module