检查插入到语句是否成功

时间:2021-03-25 15:38:01

I am using a MS Access database as the backend of my VB.NET application. I am entering users details into the database using an INSERT INTO statement:

我正在使用一个MS Access数据库作为VB的后端。网络应用程序。我使用INSERT into语句将用户详细信息输入数据库:

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 has actually been entered into the database. I have 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 doesn't return anything so this doesn't work. Any suggestions?

但是显然,insert语句不返回任何东西,所以这个语句不起作用。有什么建议吗?

3 个解决方案

#1


8  

If all you have is the INSERT statement you can use the ExecuteNonQuery() method which returns how many rows were affected.

如果只有INSERT语句,那么可以使用ExecuteNonQuery()方法,该方法返回受影响的行数。

Like this:

是这样的:

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

You have to excuse me if the VB isn't correct, I didn't test it, but I hope you get the idea.

如果VB不正确,请原谅,我没有测试它,但是我希望你们能理解。

#2


0  

Can you run a quick SELECT COUNT(*) FROM blah blah blah using the same key criteria as you used for the insert ?

您是否可以使用与插入所用的相同的键条件,从blah blah blah中运行快速选择COUNT(*) ?

#3


0  

Use the ExecuteNonQuery method, as the query doesn't return any result. The method returns an integer which is the number of rows affected.

使用ExecuteNonQuery方法,因为查询不会返回任何结果。该方法返回一个整数,该整数是受影响的行数。

Dim count As Integer = cmd.ExecuteNonQuery()

#1


8  

If all you have is the INSERT statement you can use the ExecuteNonQuery() method which returns how many rows were affected.

如果只有INSERT语句,那么可以使用ExecuteNonQuery()方法,该方法返回受影响的行数。

Like this:

是这样的:

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

You have to excuse me if the VB isn't correct, I didn't test it, but I hope you get the idea.

如果VB不正确,请原谅,我没有测试它,但是我希望你们能理解。

#2


0  

Can you run a quick SELECT COUNT(*) FROM blah blah blah using the same key criteria as you used for the insert ?

您是否可以使用与插入所用的相同的键条件,从blah blah blah中运行快速选择COUNT(*) ?

#3


0  

Use the ExecuteNonQuery method, as the query doesn't return any result. The method returns an integer which is the number of rows affected.

使用ExecuteNonQuery方法,因为查询不会返回任何结果。该方法返回一个整数,该整数是受影响的行数。

Dim count As Integer = cmd.ExecuteNonQuery()