1

我有一个论坛,我希望用户在其中输入个人资料名称,然后单击添加,然后它将他们在文本框中输入的内容添加到我表中的 ProfileName 字段中。当我单击“添加”按钮时,它会出现一个错误,指出System.Data.OleDb.OleDbException在 system.data.dll 中发生类型未处理的异常。这是我的代码:

Private Sub RefreshData()
    Dim cnn As New OleDb.OleDbConnection
    If Not cnn.State = ConnectionState.Open Then
        cnn.Open()
    End If
    Dim da As New OleDb.OleDbDataAdapter("SELECT id AS [ID], " & _
        "ProfileName AS [Name] " & _
        " FROM Profile ORDER BY id", cnn)
    Dim dt As New DataTable
    da.Fill(dt)
    cnn.Close()
    Me.dgvData.DataSource = dt
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)                 Handles Button1.Click
    If (TextBox1.Text = "") Then
        MsgBox("Please enter a profile name.")
    Else
        Dim cnn As New OleDb.OleDbConnection("Provider=Microsof… Source=c:\Data\Database.mdb     ;Extended Properties=Paradox 5.x;")
        Dim cmd As New OleDb.OleDbCommand
        If Not cnn.State = ConnectionState.Open Then
            cnn.Open()
        End If

        cmd.Connection = cnn
        cmd.CommandText = "INSERT INTO Profile(ProfileName) " & _
            "VALUES (" & Me.TextBox1.Text & "')"
        cmd.ExecuteNonQuery()

        cnn.Close()
        Dim oForm As addsnake
        oForm = New addsnake
        oForm.Show()
        oForm = Nothing
        Me.Close()
    End If
End Sub
4

1 回答 1

1

在没有更多信息的情况下,我建议:

你不需要给 id 一个 ID 的别名:

SELECT id AS [ID]

但是,如果您愿意,您仍可以将其括在方括号中:

SELECT [id]

您在以下行中缺少撇号:

"VALUES (" & Me.TextBox1.Text & "')"

还要在connectionstrings.com检查您的连接字符串

于 2013-09-19T20:20:04.007 回答