Insert/Update/Delete Data Using ExecuteNonQuery
Submitted by GeePee on Saturday, May 30, 2015 - 00:04.
Unlike DataReader, ExecuteNonQuery function allows you to insert, update, and delete data. It executes a query that does not return any value.
Consider the following example:
We use function in this code to determine if the query executes without error. Those it returns TRUE if no error occurs, or FALSE if it catches any error.
Here’s an example to call this function:
- 'Execute Non Query
- Public Function ExecNonQuery(ByVal strSQL As String)
- Dim conn As OleDbConnection
- conn = New OleDbConnection
- Try
- With conn
- .ConnectionString = cnString
- .Open()
- End With
- Dim cmd As OleDbCommand = New OleDbCommand(strSQL, conn)
- cmd.ExecuteNonQuery()
- Return True
- Catch ex As OleDbException
- Return False
- Finally
- End Try
- End Function
- ExecNonQuery(“INSERT INTO Table1 (Field1, Field2, Field2) VALUES (‘Value1’, ‘Value2’, ‘Value3’);)
Add new comment
- 176 views