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.

Slides:



Advertisements
Similar presentations
17. Data Access ADO.Net Architecture New Features of ADO.NET
Advertisements

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.
Ch. 101 DataTable Data Table Object A DataTable object holds the contents of a table as a rectangular array. A data table is similar to a two-dimensional.
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 Programming. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB)
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.
Ch. 101 Changing the Contents of a Database. Ch. 102 Changing the Contents of a Database Data grid views can also be used to add, modify, and delete records.
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.
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.
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
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.
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.
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.
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.
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
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. 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 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.
9-1 Chapter 9 Working with Databases in VB.NET. 9-2 Learning Objectives Understand how databases are used to store business data and how they differ from.
Chapter 9 Using the SqlDataSource Control. References aspx.
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.
ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances.
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.
1 Introduction to ADO.NET Microsoft ADO.NET 2.0 Step by Step Rebecca M Riordan Microsoft Press, 2006.
ASP.NET Rina Zviel-Girshin Lecture 5
Christopher M. Pascucci.NET Programming: Databases & 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.
Mauricio Featherman, Ph.D. Washington St. University
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
DEV383 The ADO.NET DataSet and You Jackie Goldstein General Manager Renaissance Computer Systems
ASP.NET - insert - delete -update DataTables (disconnected datasets) Shopping Basket.
Module 3: Working with Local Data. Overview Using DataSets Using XML Using SQL Server CE.
1 11/15/05CS360 Windows Programming ADO.NET Continued.
HNDIT Rapid Application Development
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
Using ADO.Net to Build a Login System Dr. Ron Eaglin.
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.
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.
DAT 390 Advanced ADO.NET Programming Techniques Jackie Goldstein Renaissance Computer Systems
Coding ADO.NET Objects: Connection, Command, DataReader
ADO.NET Framework.
Introduction to VB programming
VB.NET Using Database.
CIS16 Application Programming with Visual Basic
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
Coding ADO.NET Objects: Connection, Command, DataReader
PROG Advanced Web Apps 4/13/2019 Programming Data Pages Wendi Jollymore, ACES.
Database Handling Class and Service
VB Classes ISYS 512.
Introduction to ADO.NET
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

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 using the Adapter object. 2. Using Command object’s ExecuteNonQuery to run the SQL statements directly on the database.

Adding a New Record to a DataTable A DataTable consists of many rows. Each of these rows is represented by a DataRow object, and is held within the DataTable Rows property. To add a new row: –Define a DataRow object. –Use DataTable’s NewRow() method to create the new row. –Assign value to fields in the new row. –Use the ADD method of the Rows to add the new row to the DataTable.

Code Example dim objNewRow as DataRow objTable=objDataSet.Tables("Cust") objNewRow = objTable.NewRow() objNewRow("cid")=cid.text objNewRow("Cname")=cname.text objNewRow("city")=city.text objNewRow("rating")=rating.text objTable.Rows.Add(objNewRow)

Add the New Row to the Data Source The Adapter object provides two-way interaction between the database and the DataSet. It has SelectCommand, InsertCommand, DeleteCommand, UpdateCommand properties that hold command objects to run SQL statements. To initialize these properties, we can: – define a command object( typically for SelectCommand) – use a CommandBuilder object (for other commands) The Update method of an Adapter object executes commands to update the database.

OleDBCommandBuilder Property: –Adapter Methods: –GetDeleteCommand –GetInsertCommand –GetUpdateCommand The commandBuilder uses the Adapter’s SelectCommand to generate Insert, Delete, and Update commands. Adapter’s InsertCommand, DeleteCommand, UpdateCommand properties generated by the Command Builder object can transfer multiple updates back to the database.

Example Using the CommandBuilder dim objBlder=new OleDBCommandBuilder objBlder=new OleDBCommandBuilder(objAdapter) objAdapter.InsertCommand=objBlder.GetInsertCommand() objAdapter.Update(objDataset,"cust") Note: The commandBuilder uses the Adapter’s SelectCommand to generate the Insert command.

DataTable’s Select Method Select(FilterExpression) –Gets an array of all System.Data.DataRow objects that match the filter criteria

Select Method Example dim objRows() as DataRow objRows=objTable.select("cid= '" & CidList.selectedItem & "'") cid.text=objRows(0).item("cid") cname.text=objRows(0).item("cname") city.text=objRows(0).item("city") rating.text=objRows(0).item("rating")

Modifying An Existing Record Use Table’s Select method to select the records for change. Assign new values to selected records. Apply the update to the database.

Modification Example objTable=objDataset.tables("cust") objRows=objTable.select("cname= '" & CnameList.selectedItem.text & "'") objRows(0).item("cid")=cid.text objRows(0).item("cname")=cname.text objRows(0).item("city")=city.text objRows(0).item("rating")=rating.text strSQL = "select * from customer;" dim objAdapter as new OledbDataAdapter(strSQL, objConn) dim objBlder=new OleDBCommandBuilder objBlder=new OleDBCommandBuilder(objAdapter) objAdapter.UpdateCommand=objBlder.GetUpdateCommand() objAdapter.Update(objDataset,"cust")

Modification Example 2: Use Table’s Rows Property

Example 2: Use Table’s Rows Property Form Load and Next Button Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load cid.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("cid") cname.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("cname") city.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("city") rating.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("rating") End Sub Private Sub NextButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click objDataSet.Tables("customer").Rows(rowIndex).Item("cid") = cid.Text objDataSet.Tables("customer").Rows(rowIndex).Item("cname") = cname.Text objDataSet.Tables("customer").Rows(rowIndex).Item("city") = city.Text objDataSet.Tables("customer").Rows(rowIndex).Item("rating") = rating.Text rowIndex += 1 cid.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("cid") cname.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("cname") city.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("city") rating.Text = objDataSet.Tables("customer").Rows(rowIndex).Item("rating") End Sub

Update Button Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 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 objAdapter As New OleDbDataAdapter(strSQLCust, objConn) Dim objBlder = New OleDbCommandBuilder() objBlder = New OleDbCommandBuilder(objAdapter) objAdapter.UpdateCommand = objBlder.GetUpdateCommand() objAdapter.Update(objDataSet, "CUSTOMER") End Sub

How.NET Keeps Track of Updates in the DataSet Each column in every row of a table maintains DataRowVersion: –Original: The value in the column when the DataTable was created and filled with data. –Current: The actual column value after changes have been made. Each row in a table maintains RowState: –Unchanged –Added –Modified –Deleted

DataRowVersion Demo –objTable.Rows(0)("rating")="E" –Messagebox.Show("Original rating is: " + objTable.Rows(0)("rating", DataRowVersion.Original)) –Messagebox.Show("New rating is: " + objTable.Rows(0)("rating", DataRowVersion.Current))

RowState Example DataSet object’s GetChanges method returns a DataSet that contains updated rows. –objModifiedDS=objDataSet.GetChanges(DataRowState.Modified) –objAddedDS= objDataSet.GetChanges(DataRowState.Added) –objDeletedDS= objDataSet.GetChanges(DataRowState.Deleted) DataTable object’s GetChanges method returns a DataTable that contains updated tows.

Deletion The dataRow’s Delete method changes the RowState to Deleted. Apply the deletions to the database.

Delete Example objTable=objDataset.tables("cust") objRows=objTable.select("cname= '" & CnameList.selectedItem.text & "'") objRows(0).Delete() strConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection(strConn) strSQL = "select * from customer;" dim objAdapter as new OledbDataAdapter(strSQL, objConn) dim objBlder=new OleDBCommandBuilder objBlder=new OleDBCommandBuilder(objAdapter) objAdapter.DeleteCommand=objBlder.GetDeleteCommand() objAdapter.Update(objDataset,"cust")

Use Table’s Rows with Index Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click objDataSet.Tables("customer").Rows(rowIndex).Delete() rowIndex += 1 End Sub