Verifying successful execution of an INSERT INTO statement

I use the MS Access database as the base of my VB.NET application. I entered user data into the database using the INSERT INTO statement:

cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & config("DatabasePath") & ";") cn.Open() cmd = New OleDbCommand("INSERT INTO blah blah blah...", cn) dr = cmd.ExecuteReader 

Everything works, but I wanted to check if the data was actually entered into the database. I tried using:

 cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & config("DatabasePath") & ";") cn.Open() cmd = New OleDbCommand("INSERT INTO blah blah blah...", cn) dr = cmd.ExecuteReader If dr.Read() Then ' Blah End If 

but obviously, the insert statement does not return anything, so this will not work. Any suggestions?

+6
database insert ms-access
source share
3 answers

If all you have is an INSERT statement, you can use the ExecuteNonQuery() method, which returns the number of rows.

Like this:

 cmd = New OleDbCommand("INSERT INTO blah blah...", cn) rowCount = cmd.ExecuteNonQuery() If rowCount < 1 Then ' Blah 

You must excuse me if VB is wrong, I have not tested it, but I hope you understand this idea.

+8
source share

Is it possible to run a quick SELECT COUNT(*) FROM blah blah blah using the same key criteria as for insertion?

0
source share

Use the ExecuteNonQuery method since the query does not return a result. The method returns an integer, which is the number of rows affected.

 Dim count As Integer = cmd.ExecuteNonQuery() 
0
source share

All Articles