0

我试图向 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
4

2 回答 2

0

MSAccess 中没有 LAST_INSERT_ID 功能。试试吧

"SELECT @@IDENTITY"

话虽如此,我强烈建议您更改原始命令以使用参数化查询。它始终是一种安全的数据库命令方法,因为您不必担心正确引用字符串、数字和日期,但最重要的是,您可以避免 Sql Injection

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)

作为旁注。您正在捕获 ODBC 异常,但您的代码使用 OleDb。(以前版本的遗留物?)

知道了

此行执行第一个查询,而不是第二个

Dim cmd2 As New Data.OleDb.OleDbCommand(vSQL2, vConnStr)
                                            ^

最后使用 cmd3 实例统计客户

Dim vCount3 As Integer = cmd3.ExecuteScalar()
MessageBox.Show("***** Total Customers " & vCount3)
于 2013-09-25T11:30:11.950 回答
0

查看这篇文章autoincremented-value,它在 2000 或更高版本中的工作方式似乎有所不同。

另外,我认为您的变量有问题。您将 vSQL 与 cmd 一起使用,但您也将 vSQL 与 cmd2 一起使用。所以,当你做你的CInt(cmd2.ExecuteScalar()),你实际上是在再次运行你的插入。尝试将你的东西组合在一起,这样更容易理解,如下所示:

Dim vSQL As String = "  Insert into Customer "
vSQL = vSQL & " (FirstName, LastName, Telephone, Email)"
vSQL = vSQL & " Values ("
vSQL = vSQL & "'" & cusFName & "'"
vSQL = vSQL & "," & "'" & cusLName & "'"
vSQL = vSQL & "," & cusTP
vSQL = vSQL & "," & "'" & cusEmail & "'"
vSQL = vSQL & ");"

Dim cmd As New Data.OleDb.OleDbCommand(vSQL, vConnStr)
Dim vRowsUpdated As Integer = cmd.ExecuteNonQuery
MessageBox.Show("Successfully saved to the system" & vRowsUpdated)

Dim vSQL2 As String = "SELECT LAST_INSERT_ID()"
Dim cmd2 As New Data.OleDb.OleDbCommand(vSQL2, vConnStr)
Dim vCusId As Integer = CInt(cmd2.ExecuteScalar())
MessageBox.Show("*** ID: " & vCusId)

这将有助于您在声明之前不能使用 vSQL2 或 cmd2 并删除 1/2 的可能错误。

于 2013-09-25T14:22:57.523 回答