我需要能够添加,但有一个错误。 SYNTAX ERROR INSERT INTO和cmd.ExecuteNonQuery()

时间:2016-04-21 00:36:23

标签: vb.net

    Dim cmd As New OleDb.OleDbCommand
    If Not cnn.State = ConnectionState.Open Then
        'open connection if it is not yet open
        cnn.Open()
    End If

    cmd.Connection = cnn
    'check whether add new or update
    If Me.txtcheckID.Tag & "" = "" Then
        'add new 
        'add data to table
        cmd.CommandText = "INSERT INTO student(checkid, branch, amount, type, status, date, address) " & _
                        " VALUES(" & Me.txtcheckID.Text & ",'" & Me.txtBranch.Text & "','" & _
                        Me.cboType.Text & "','" & Me.txtStatus.Text & "','" & _
                        Me.txtAddress.Text & "','" & Me.txtDate.Text & "','" & Me.txtAmount.Text & "')"
        cmd.ExecuteNonQuery()
    Else
        'update data in table
        cmd.CommandText = "UPDATE student " & _
                    " SET checkid=" & Me.txtcheckID.Text & _
                    ", branch='" & Me.txtBranch.Text & "'" & _
                    ", type='" & Me.cboType.Text & "'" & _
                    ", status='" & Me.txtStatus.Text & "'" & _
                    ", address='" & Me.txtAddress.Text & "'" & _
                    ", amount='" & Me.txtAmount.Text & "'" & _
                    ", date='" & Me.txtDate.Text & "'" & _
                    " WHERE checkid=" & Me.txtcheckID.Tag
        cmd.ExecuteNonQuery()
    End If
    'refresh data in list
    RefreshData()
    'clear form
    Me.btnClear.PerformClick()

    'close connection
    cnn.Close()

1 个答案:

答案 0 :(得分:0)

cmd.CommandText = "INSERT INTO student(checkid, branch, amount, type, status, date, address) " & _
                    " VALUES(" & Me.txtcheckID.Text & ",'" & Me.txtBranch.Text & "','" & _
                    Me.cboType.Text & "','" & Me.txtStatus.Text & "','" & _
                    Me.txtAddress.Text & "','" & Me.txtDate.Text & "','" & Me.txtAmount.Text & "')"

很抱歉,如果我是愚蠢的,但在地址之间的CommandText字符串中有2个空格)和第一个换行错误导致的VALUES?不确定VB和OleDB如何处理空格,我的帐户太新了,无法发表评论......