语法错误! > _<

时间:2013-12-06 03:52:50

标签: vb.net

Guy's Cant似乎在VB.Net中发现了我的错误!我的代码没事!但是这个函数仍然不会在我的Insert into语句中保存数据库导致语法错误!请帮我 ! :d

这是我的代码!:D!

Dim Transaction As OleDb.OleDbTransaction = Nothing
Dim Connection As OleDb.OleDbConnection = Nothing
Try

    Connection = New OleDb.OleDbConnection(My.Settings.POS_CanteenConnectionString)
    Connection.Open()
    Transaction = Connection.BeginTransaction
    Dim SQL As String = "Insert into Recipt (ReciptDate,ReciptTotal)Values(:0,:1)"
    Dim CMD1 As New OleDb.OleDbCommand
    CMD1.Connection = Connection
    CMD1.Transaction = Transaction
    CMD1.CommandText = SQL
    CMD1.Parameters.AddWithValue(":0", Now.Date)
    CMD1.Parameters.AddWithValue(":1", TextBox4.Text)
    CMD1.ExecuteNonQuery()
    CMD1.Dispose()
    SQL = "Select Max(ReciptID) As MAXID from Recipt"
    Dim CMD2 As New OleDb.OleDbCommand
    CMD2.Connection = Connection
    CMD2.Transaction = Transaction
    CMD2.CommandText = SQL
    Dim ReciptID As Long = CMD2.ExecuteScalar()
    CMD2.Dispose()
    Dim I As Integer
    For I = 0 To DGV3.Rows.Count - 1
        Dim BarCode As String = DGV3.Rows(I).Cells(0).Value
        Dim BuyPrice As Decimal = DGV3.Rows(I).Cells(2).Value
        Dim SellPrice As Decimal = DGV3.Rows(I).Cells(3).Value
        Dim ItemCount As Integer = DGV3.Rows(I).Cells(4).Value
        Dim CMD3 As New OleDb.OleDbCommand
        SQL = "Insert to ReciptDetails" & _
              "(ReciptID,BarCode,ItemCount,ItemBuyPrice,ItemSellPrice)" & _
              "Values" & _
              "(:0          ,:1           ,:2          ,:3          ,:4          )"
        CMD3.Connection = Connection
        CMD3.Transaction = Transaction
        CMD3.CommandText = SQL
        CMD3.Parameters.AddWithValue(":0", ReciptID)
        CMD3.Parameters.AddWithValue(":1", BarCode)
        CMD3.Parameters.AddWithValue(":2", BuyPrice)
        CMD3.Parameters.AddWithValue(":3", ItemCount)
        CMD3.Parameters.AddWithValue(":4", SellPrice)

        CMD3.ExecuteNonQuery()
        CMD3.Dispose()

    Next
    Transaction.Commit()

    Transaction.Dispose()
    Connection.Close()
    Connection.Dispose()
    DGV3.Rows.Clear()
    TextBox4.Text = ""
Catch ex As Exception

    If Transaction IsNot Nothing Then
        Transaction.Rollback()
    End If
    If Connection IsNot Nothing Then
        If Connection.State = ConnectionState.Open Then
            Connection.Close()
        End If

    End If
    MsgBox(ex.Message, MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "ERROR")

End Try

1 个答案:

答案 0 :(得分:1)

首先,我认为:前缀不会调用sp参数,而是应该使用@。因此,你需要用;替换.AddWithValue(无处不在);

CMD1.Parameters.AddWithValue("@0", Now.Date)
CMD1.Parameters.AddWithValue("@1", TextBox4.Text)

其次,我认为插入语法不对,您放置了TO而不是INTO并且缺少空格。试试这个;

 SQL = "Insert into ReciptDetails " & _
       "(ReciptID, BarCode, ItemCount, ItemBuyPrice, ItemSellPrice)" & _
       " Values (@0, @1, @2, @3, @4)"