检查INSERT INTO语句是否成功 [英] Checking if INSERT INTO statement was successful
问题描述
我使用MS Access数据库作为我的VB.NET应用程序的后端。我使用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
一切正常,但我想检查数据实际上已输入到数据库中。我尝试使用:
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
如果dr.Read()then
'Blah
结束如果
但显然insert语句不返回任何东西,所以这不工作。任何建议?
如果您拥有的是INSERT语句,您可以使用 ExecuteNonQuery()
cmd = New OleDbCommand(INSERT INTO blah blah ...,cn)
rowCount = cmd.ExecuteNonQuery()
如果rowCount < 1 Then
'Blah
如果VB不正确,我没有测试,但我希望你能得到这个想法。
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:
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?
If all you have is the INSERT statement you can use the ExecuteNonQuery()
method which returns how many rows were affected.
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.
这篇关于检查INSERT INTO语句是否成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!