我试图向 MS ACCESS DB 插入值,但我无法向 ACCESS 插入任何数据。我将 CustomerID 作为具有自动增量的主键。运行代码后它总是给我客户总数 0 * 请帮我弄清楚这里出了什么问题!
这是我的代码
Public Const settings As String = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=C:\Users\sha\Documents\Visual Studio 2010\Projects\Assign2_24\Assign2_24\db24.accdb;"
Dim vConnStr As New OleDbConnection(settings)
If vboo = "" Then
Try
vConnStr.Open()
Dim vSQL As String = "Insert into Customer "
vSQL = vSQL & " (FirstName, LastName, Telephone, Email)"
vSQL = vSQL & " Values (?, ?, ?, ?)"
Dim cmd As New Data.OleDb.OleDbCommand(vSQL, vConnStr)
cmd.Parameters.AddWithValue("@p1", cusFName)
cmd.Parameters.AddWithValue("@p2", cusLName)
cmd.Parameters.AddWithValue("@p3", cusTP)
cmd.Parameters.AddWithValue("@p4", cusEmail)
Dim vRowsUpdated As Integer = cmd.ExecuteNonQuery
MessageBox.Show("Successfully saved to the system" & vRowsUpdated)
'*************************************** Retrieve the CusID that have inserted *******************************
Dim vSQL2 As String = "SELECT @@IDENTITY"
Dim cmd2 As New Data.OleDb.OleDbCommand(vSQL2, vConnStr)
Dim vCusId As Integer = CInt(cmd2.ExecuteScalar())
MessageBox.Show("*** ID: " & vCusId)
'Dim vCusId As Integer = CInt(cmd2.ExecuteScalar())
'MessageBox.Show("*** ID: " & vCusId)
Dim vSQL3 As String = "Select count (*) from Customer"
Dim cmd3 As New Data.OleDb.OleDbCommand
cmd3.CommandText = vSQL3
Dim vCount3 As Integer = cmd.ExecuteScalar()
MessageBox.Show("***** Total Customers " & vCount3)
Catch ex As Data.Odbc.OdbcException
Dim vErMsg As String = " *** Error occured while registering the customer ***"
vErMsg = vErMsg & ex.ErrorCode & "***" & ex.Message & "***"
Finally
vConnStr.Close()
End Try