VB.NET SQL Update无法正常工作

时间:2018-04-02 14:47:56

标签: sql vb.net

我试图让这个SQL Update代码工作几天,我无法做到,有人能帮助我吗?

    dbAccess.AddParameter("@flashcardID", flashcardUpdateIndex)
    dbAccess.AddParameter("@flashcardFront", txtFlashcardFront.Text)
    dbAccess.AddParameter("@flashcardBack", txtFlashcardBack.Text)

    dbAccess.ExecuteQuery("UPDATE Questions SET Flashcard_Front=@flashcardFront WHERE Question_ID=@flashcardID;")

然后这是我创建的类dbAccess

Imports System.Data.OleDb

Public Class DBControl

'create database connection'
Private DBConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Project_DB.mdb")

'create database command'
Private DBCommand As OleDbCommand

'storing data'
Public DBDA As OleDbDataAdapter
Public DBDT As DataTable

'query parameters'
Public parameters As New List(Of OleDbParameter)

'query stats'
Public recordcount As Integer
Public exception As String

Public Sub ExecuteQuery(Query As String)

    'reset query stats'
    recordcount = 0
    exception = ""

    'continue
    Try
        'open connection'
        DBConnection.Open()

        'create database command'
        DBCommand = New OleDbCommand(Query, DBConnection)

        'load parameters into database command'
        parameters.ForEach(Sub(p) DBCommand.Parameters.Add(p))

        'clear parameter list'
        parameters.Clear()

        'execute command and fill datatable'
        DBDT = New DataTable
        DBDA = New OleDbDataAdapter(DBCommand)
        recordcount = DBDA.Fill(DBDT)

    Catch ex As Exception
    End Try

    'close connection to database'
    If DBConnection.State = ConnectionState.Open Then
        DBConnection.Close()
    End If

End Sub

Public Sub AddParameter(name As String, value As Object)

    'include query and command parameters'
    Dim newParameter As New OleDbParameter(name, value)
    parameters.Add(newParameter)
End Sub

End Class

如果有人能够提供帮助那就太棒了! INSERT,DELETE,SELECT按预期工作,只是UPDATE

1 个答案:

答案 0 :(得分:0)

在评论中提到的 Lars , 您的参数必须按照与sql语句相同的顺序添加

dbAccess.AddParameter("@flashcardFront", txtFlashcardFront.Text)
dbAccess.AddParameter("@flashcardID", flashcardUpdateIndex)
' rem this line out for now - dbAccess.AddParameter("@flashcardBack", txtFlashcardBack.Text)

dbAccess.ExecuteQuery("UPDATE Questions SET Flashcard_Front=@flashcardFront WHERE Question_ID=@flashcardID;")

另外,在下面的类代码中,您将异常声明为字符串? 将该行更改为

dim ex as exception

然后进入ExecuteQuery子例程,这就是你使用它的地方,那就是它应该存在的地方, UNLESS 你想要访问异常信息(如果有的话) OUTSIDE 那个子程序。

Imports System.Data.OleDb

Public Class DBControl

    'create database connection'
    Private DBConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Project_DB.mdb")

    'create database command'
    Private DBCommand As OleDbCommand

    'storing data'
    Public DBDA As OleDbDataAdapter
    Public DBDT As DataTable

    'query parameters'
    Public parameters As New List(Of OleDbParameter)

    'query stats'
    Public recordcount As Integer

    Public Sub ExecuteQuery(Query As String)

        Dim ex As exception

        'reset query stats'
        recordcount = 0
        'rem REMOVE THIS LINE PERMANENTLY -     exception = ""

        'continue'
        Try
            'open connection'
            DBConnection.Open()

            'create database command'
            DBCommand = New OleDbCommand(Query, DBConnection)

            'load parameters into database command'
            parameters.ForEach(Sub(p) DBCommand.Parameters.Add(p))

            'clear parameter list'
            parameters.Clear()

            'execute command and fill datatable'
            DBDT = New DataTable
            DBDA = New OleDbDataAdapter(DBCommand)
            recordcount = DBDA.Fill(DBDT)

        Catch ex As Exception
            msgbox (ex.message)
        End Try

        'close connection to database'
        If DBConnection.State = ConnectionState.Open Then
            DBConnection.Close()
        End If

    End Sub

    Public Sub AddParameter(name As String, value As Object)
        'include query and command parameters'
        Dim newParameter As New OleDbParameter(name, value)
        parameters.Add(newParameter)
    End Sub

End Class