Share Your Source Code or Article

Do you have source code, articles, tutorials, web links, and books to share? You can write your own content here. You can even have your own blog.

Submit now...

Database Programming Made Easy

This tutorial will teach you step by step on how to connect and manipulate database. If you'd like to suggest a tutorial please write a comment at the bottom of this article.

Read more...

Hire Us to Do Your Work

Do you want a customized system? Do you want to setup your own website to do business? Then we are here to help you in your programming needs.

Read more...

Search

How to Modify Data Using ExecuteNonQuery
admin's picture


0
Your rating: None
Language: 

This tutorial is part of Database Programming Made Easy Series.

Modify Data using ExecuteNonQuery

In my Hotel Reservation System (VB .NET version) I’m using ExecuteNonQuery sometimes if the data to be passed to the data source is small. Like inserting or updating a single row. You can use ExecuteNonQuery even if you want to delete multiple rows.

I recommend modifying a data using Dataset if the statement is too complicated or contains a lot of fields.
An example of using ExecuteNonQuery:

  1. ExecNonQuery("INSERT INTO Customers (CustomerID, LastName, FirstName) Values (" & CustomerID & ", '" & txtLastName.Text & "', '" & txtFirstName.Text & "')")

This code was taken from hotel reservation system under “Check In” form and within the SaveRecord procedure. Take note that ExecNonQuery is just a Function.

Going back from our sample code let us use the code from Data Reader and remove unnecessary line. The code to execute a non query command should look like this:

  1. 'Set up connection string
  2. Dim cnString As String
  3.  
  4. txtStatus.Text = "Connecting to database using MS Access" & vbCrLf & vbCrLf
  5.  
  6. cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\data\Northwind.mdb"
  7.  
  8. 'Sql statement. This will insert record into Customers table
  9. Dim sqlQRY As String = "INSERT INTO Customers (CustomerID, CompanyName, City, Region, PostalCode) Values ('AAAAA', 'ABC Company', 'Seattle', 'WA', '98128')"
  10.  
  11. 'Create connection
  12. Dim conn As OleDbConnection = New OleDbConnection(cnString)
  13.  
  14. Try
  15. ' Open connection
  16. conn.Open()
  17.  
  18. 'create command
  19. Dim cmd As OleDbCommand = New OleDbCommand(sqlQRY, conn)
  20.  
  21. 'execute non query command
  22. cmd.ExecuteNonQuery()
  23.  
  24. 'Sql statement. This will delete newly inserted record from Customers table
  25. sqlQRY = "DELETE FROM Customers WHERE CustomerID='AAAAA'"
  26.  
  27. 'execute non query command AGAIN! By this time using DELETE statement
  28. cmd.CommandText = sqlQRY
  29.  
  30. 'execute non query command. To see the changes in insert statement, comment the code below
  31. cmd.ExecuteNonQuery()
  32.  
  33. Catch ex As SqlException
  34. txtStatus.Text = "Error: " & ex.ToString & vbCrLf
  35. Finally
  36. ' Close connection
  37. conn.Close()
  38. txtStatus.Text = txtStatus.Text & vbCrLf & "Connection closed."
  39. End Try

As you can see the only difference is the  cmd.ExecuteNonQuery() and its SQL Statement above after the connection string “cnString”.

ExecuteNonQuery command is very useful if you want a quick way of modifying a data in your data sources. Just replace the INSERT command to something like UPDATE or DELETE command if you want to execute update or delete statement.

Previous: How to Modify Data

Next: How to Modify Data Using Datasets and Data Adapters


Download Code



Post new comment

  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <java>, <java5>, <javascript>, <mysql>, <php>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • Links to specified hosts will have a rel="nofollow" added to them.

  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Step by Step Java Tutorial

In this tutorial you will learn how to program with Java. It has a rich of information to be educated well with Java.

Read more...

Do You Have Question?

Do you have any question related to computer programming? Visit our forum and post new topic on the category you like. Be gentle when asking a question.

Ask now...

Point of Sale

Point of Sale is very useful especially for supermarkets or restaurants. I have included a barcode scanner in this program. Please check it out.

Read more...