ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
FAQ
Login
 
ASP Dot Net Tutorial - Index
ASP Dot Net Tutorial - Microsofts ASP.NET
ASP Dot Net Tutorial - Begining Web Forms
ASP Dot Net Tutorial - User Controls
ASP Dot Net Tutorial - ADO .NET
ASP Dot Net Tutorial - DataBinding to WebControls
ASP Dot Net Tutorial - DataList Controls
ASP Dot Net Tutorial - DataGrid Control
ASP Dot Net Tutorial - Advance Practical Examples on DataGrid
ASP Dot Net Tutorial - Working with DataSets
ASP Dot Net Tutorial - Working with XML
ASP Dot Net Tutorial - Forms Based Authentication
ASP Dot Net Tutorial - Windows Based Authentication
     

 
Online Personal Trainer Certification
Test Search by Job Title
Find a Personal Trainer
Job Resources
Free Self Tests and Quizzes
Find a Lawyer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Six Sigma Green Belt Certification
Lean Management Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
SEO Certification
Online Photography Course & Certification
Online Business Writing Course & Certification
Baby Sitting Course & Certification
Time Manangement Certification
Health Club Management Certification
Selling Skills Certification
Forensic Skills Certification
Green Living Course  free!
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > ASP.NET Tutorial > ADO.NET
 

ASP.NET Tutorial - ADO.NET

                                                                                                                Page 9 of 14
 

In the above example there are two TextBox controls named txtbookName and txtAuthor. When the Add Books! Button, are clicked the Button Click subroutine is executed, and the values entered into the two TextBox controls are inserted into the Books table of MS Access database which is located at c:\Books.mdb.

Updating Database Records

To update the database record in the table the UPDATE command is used as: -

UPDATE table_name SET columnl = valuel, column2 = value2... WHERE condition

Suppose you want to update a table Book in which you want to Update the name of the Author from “John” to “David”. The code used would be like: -

Update books

Set Author=’David’

Where Author=’John’

To use UPDATE COMMAND follow these steps: -

1.Create and open a database connection.

2.Create a database command that represents the SQL Update statement to execute.

3.Execute the command with the ExecuteNonQuery () method.

The following example shows how the books are updated.

Example 29 SqlUpdateDemo.aspx

<%@ Import Namespace="System.Data.SqlClient" %>

<%

Dim conNorthwind As SqlConnection

Dim strUpdate As String

Dim cmdUpdate As SqlCommand

conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;database=Northwind" )

strUpdate = "Update Books Set Author='David' Where Author='John'"

cmdUpdate = New SqlCommand( strUpdate, conNorthwind )

conNorthwind. Open ()

cmdUpdate. ExecuteNonQuery ()

conNorthwind. Close ()

%>

 

Author Updated in the Table Books!

The output of this example is shown below: -

The first statement in the above example imports the necessary namespace for working with the SQL ADO.NET classes.

Next, a database connection is initialized with the correct connec­tion string for the local server. In the statement that follows, passing a SQL Update command and SqlConnection to the constructor for the class creates an instance of the SqlCommand class.

The SQL Update command is executed when the ExecuteNonQuery () method of the SqlCommand class is called. At this point, the Update statement is transmitted to SQL Server and executed.

The example above shows that the code above works only with Microsoft SQL Server (version 7.0 and higher). If you want to update a record in a Microsoft Access database table, then you need to use the ADO.NET classes from system.Data.OleDb namespace rather than the System.Data.SqlClient namespace as shown below: -

Example 30 OleDbUpdateDemo.aspx

<%@ Import Namespace="System.Data.OleDb" %>

<%

Dim conAuthors As OleDbConnection

Dim strUpdate As String

Dim cmdUpdate As OleDbCommand

conAuthors= New OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=c:\Books.mdb")

strUpdate = "Update Books Set Author='David' Where Author='John'"

cmdUpdate = New OleDbCommand( strUpdate, conAuthors )

conAuthors. Open ()

cmdUpdate. ExecuteNonQuery ()

conAuthors. Close ()

%>

Author Updated in the Table Books!

 

The output of this example is as shown below:-

The first statement in the above example imports the necessary namespace for working with the OleDb ADO.NET classes. Next, a database connection is initialized with the correct connec­tion string for the local server. In the statement that follows, passing a SQL Update command and OleDbConnection to the constructor for the class creates an instance of the OleDbCommand class.

The SQL Update command is executed when the ExecuteNonQuery () method of the OleDbCommand class is called. At this point, the Update statement is transmitted to MS Access and executed.

 

       

 
     
Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
 
© ExpertRating 2006. All Rights Reserved.