1

需要一点帮助。

我目前正在使用此代码将项目添加到 Visual Basic 2010 中的 Access 数据库

Imports System
Imports System.Data
Imports System.Collections
Imports System.Windows.Forms
Imports System.Resources
Imports System.Data.OleDb
Public Class Form2
Dim myConnection = New System.Data.OleDb.OleDbConnection()
Dim SqlCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'connects to the database when the form loads
    myConnection = New System.Data.OleDb.OleDbConnection()
    myConnection.ConnectionString = "Provider =Microsoft.ACE.OLEDB.12.0; Data Source =project.accdb; Persist Security Info =False;"
    Try
        myConnection.Open()

    Catch ex As Exception
        MsgBox("Error Please Go Back And Try Again")
        Console.WriteLine(ex.Message)

    End Try
    'adds items to the dropdown menu

    Dim result As OleDb.OleDbDataReader
    Dim sqlText As String
    Dim add1 As Integer = 0

    If myConnection.State <> ConnectionState.Open Then
        MsgBox("Connection is not open")
        Exit Sub
    End If

    sqlText = "select * from groups"
    SqlCommand = New OleDbCommand(sqlText, myConnection)
    result = SqlCommand.ExecuteReader()

    Do While result.Read()
        ComboBox1.Items.Insert(add1, result.GetValue(0))
        add1 = add1 + 1
    Loop

    result = Nothing
    SqlCommand = Nothing
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Insert button
    Dim item As String = ""
    Dim price As String = ""
    Dim group As String = ""
    Dim dateandtime As Double
    Dim dateandtime2 As String = ""
    Dim sqlInsert As String = ""
    Dim result As Integer = -1
    Dim SqlCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand

    If TextBox1.Text = "" Or TextBox2.Text = "" Then
        MsgBox("Item and Price Required!")
        Exit Sub
    End If

    item = TextBox1.Text
    price = TextBox2.Text.Trim
    group = ComboBox1.Text.Trim
    dateandtime = DateTimePicker1.Value.ToOADate()
    dateandtime2 = dateandtime.ToString
    sqlInsert = "INSERT INTO purchases (item, price, groupName, datetime) VALUES ('" + item + "', '" + price + "', '" + group + "', '" + dateandtime2 + "')"

    Try

        SqlCommand.Connection = myConnection
        SqlCommand.CommandText = sqlInsert
        result = SqlCommand.ExecuteNonQuery()

        If result = 0 Then
            MsgBox("No records inserted!")
        Else
            MsgBox(CStr(result) & " record inserted!")
        End If

        TextBox1.Clear()
        TextBox2.Clear()
        ComboBox1.Text = ""

    Catch ex As Exception
        MsgBox(ex.ToString)

    End Try

    result = -1
    SqlCommand = Nothing
End Sub

但是每次我尝试添加任何东西时都会出现这个异常查看这里:http: //i.stack.imgur.com/itd39.jpg有什么 想法吗?

4

1 回答 1

2

MystPhysX,

我意识到这个答案对于你的问题来说已经晚了几年,但是,对于任何在互联网上搜索类似问题的人(这就是我找到这个线程的方式),我想提供适用于的解决方案我的问题。

您正在使用“+”运算符连接 Visual Basic 中的 sqlInsert 字符串。我相信使用“&”运算符将解决抛出的错误。下面的链接将提供更多信息。

http://msdn.microsoft.com/en-us/library/te2585xw.aspx

这是编辑后的代码行:

sqlInsert = "INSERT INTO purchases (item, price, groupName, datetime) VALUES ('" & item & "', '" & price & "', '" & group & "', '" & dateandtime2 & "')"
于 2014-09-02T14:27:05.260 回答